__get and __set magic methods allows us to get and set any variable without having individual getter and setter methods:class Foo {
protected $properties = array();
public function __get( $key )
{
if(array_key_exists($key, $this->properties)){
return $this->properties[$key];
}
return $this->properties[ $key ];
}
public function __set( $key, $value )
{
$this->properties[ $key ] = $value;
}
}
$foo = new Foo();
$foo->name = 'David';
echo $foo->name;In this example, we can set any variable, even if it does not exist. Every variable will be addd to properties array__get and __set methods are called when there is an attempt either to use or to set a new value to any of the class propertyclass Person{
public $firstName;
public function __get($propertyName){
echo "attempted to read property: $propertyName" . PHP_EOL;
}
public function __set($propertyNane, $propertyValue){
echo "attempted to set new value to property: $propertyNane" . PHP_EOL;
}
}
$p = new Person();
$p->firstName = 'Doe';
echo $p->firstName . PHP_EOL;
$p->lastName = 'John';
echo $p->lastName;
If the property is public, the methods are not going to be called (if the property does not exist, or exists but is not public, the methods will get called)
If we make $firstName property private or protected, the methods will be called for the $firstName property
__get, __set, __call and __callStatic are invoked when the method or property is inaccessibleinterface Talkative {
public function talk();
}
class Cat implements Talkative {
public function talk() {
return 'Woof' . PHP_EOL;
}
}
class Dog implements Talkative {
public function talk() {
return 'Meow' . PHP_EOL;
}
}
class Tortoise implements Talkative {
public function talk() {
return 'Yak yak yak yak ...' . PHP_EOL;
}
}
$cat = new Cat;
$dog = new Dog;
$tortoise = new Tortoise;
echo $cat->talk();
echo $dog->talk();
echo $tortoise->talk();json_encode() class MyClass implements JsonSerializable {
public function jsonSerialize() {
return json_encode([1, 2, "44"]);
}
}
$c = new MyClass();
echo json_encode($c);
In this example after calling json_encode($c) function, JsonSerializable will be called automatically because the class MyClass implements JsonSerializableclass MyClass implements Countable {
public function count() {
return 5;
}
}
$c = new MyClass();
echo $c->count();
echo count($c); //calls $c->count();We can use count() function for the class instance, when the class implements Countable interfaceclass Lock {
private $isLocked;
public function __construct() {
}
public function lock() {
$this->isLocked = true;
}
public function unLock() {
$this->isLocked = false;
}
public function isLocked() {
return $this->isLocked;
}
}
class Chest {
private $lock;
public function __construct($lock) {
$this->lock = $lock;
}
public function close() {
$this->lock->lock();
echo 'Closed' . PHP_EOL;
}
public function open() {
if ($this->lock->isLocked()) {
$this->lock->unLock();
}
echo 'Opened' . PHP_EOL;
}
public function isClosed() {
return $this->lock->isLocked();
}
}
$chest = new Chest(new Lock);
$chest->open();
$chest->close();class Bird{
public $canFly;
public $legCount;
public $className;
public function __construct($canFly, $legCount, $className) {
$this->canFly = $canFly;
$this->legCount = $legCount;
$this->className = $className;
}
public function canFly() {
return $this->canFly;
}
public function getLegCount() {
return $this->legCount;
}
public function getBirdInfo() {
$canFly = $this->canFly() ? 'can fly' : 'can not fly';
return $this->className. ' ' . $canFly . ' and has ' . $this->getLegCount() . ' legs' . PHP_EOL;
}
}
class Pigeon extends Bird{
public function __construct($canFly, $legCount) {
parent::__construct($canFly, $legCount, get_class());
}
}
class Penguin extends Bird{
public function __construct($canFly, $legCount) {
parent::__construct($canFly, $legCount, get_class());
}
}
$pigeon = new Pigeon(true, 2);
$penguin = new Penguin(false, 2);
echo $pigeon->getBirdInfo();
echo $penguin->getBirdInfo();json_decode returns an instance of stdClass class$json = '{ "foo": "bar", "number": 42 }';
$stdInstance = json_decode($json);
//Example with StdClass
echo get_class($stdInstance) . ':' . PHP_EOL;
echo $stdInstance->foo . PHP_EOL; //"bar"
echo $stdInstance->number . PHP_EOL . PHP_EOL;
//Example with associative array
echo 'associative array:' . PHP_EOL;
$array = json_decode($json, true);
echo $array['foo'] . PHP_EOL; //"bar"
echo $array['number'] . PHP_EOL; //42
2. Casting an array to an object returns an instance of stdClass class$array = array(
'Property1'=>'hello',
'Property2'=>'world',
'Property3'=>'again',
);
$obj = (object) $array;
echo get_class($obj) . ':' . PHP_EOL;
echo $obj->Property3;
User details info represented using an array and an object$array_user = array();
$array_user["name"] = "smith john";
$array_user["username"] = "smith";
$array_user["id"] = "1002";
$array_user["email"] = "smith@nomail.com";
$obj_user = new stdClass;
$obj_user->name = "smith john";
$obj_user->username = "smith";
$obj_user->id = "1002";
$obj_user->email = "smith@nomail.com";interface Element {
public function characteristics();
}
class Water implements Element {
public function characteristics() {
return [
'Water characteristic 1',
'Water characteristic 2',
'Water characteristic 3',
];
}
}
class Fire implements Element {
public function characteristics() {
return [
'Fire characteristic 1',
'Fire characteristic 2',
'Fire characteristic 3',
];
}
}
class Air implements Element {
public function characteristics() {
return [
'Air characteristic 1',
'Air characteristic 2',
'Air characteristic 3',
];
}
}
class Earth implements Element {
public function characteristics() {
return [
'Earth characteristic 1',
'Earth characteristic 2',
'Earth characteristic 3',
];
}
}
function describe(Element $element) {
echo get_class($element) . "\n";
$characteristics = $element->characteristics();
if (is_array($characteristics)) {
foreach ($characteristics as $characteristic) {
echo $characteristic . "\n";
}
echo "\n\n";
}
}
$element = new Water;
describe($element);
$element = new Fire;
describe($element);
$element = new Air;
describe($element);
$element = new Earth;
describe($element);$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
Without quotes:$new = htmlspecialchars("<a href='test'>Test</a>");
echo $new; // <a href='test'>Test</a>
The second parameter possible values:
ENT_COMPAT - Will convert double-quotes and leave single-quotes alone.
ENT_QUOTES - Will convert both double and single quotes.
ENT_NOQUOTES - Will leave both double and single quotes unconverted.
The default is ENT_COMPAT