<?php
// MAGIC method __get called when try to access private or non existence proeprties
// MAGIC method __destruct called at end of script when desotrying object
class A {
public $var = 'test';
public function __destruct() {
echo 'destruct of A magic called at end of script';
}
}
$a = new A();
echo $a->var;
echo '<br/>';
class B {
private $var2 = 'test2';
public $var3 = 'test3';
public function __get($key) {
echo 'get magic method';
return $this->$key;
}
public function __destruct() {
echo 'destruct of B magic called at end of script';
}
}
$b = new B();
echo $b->var2;
echo '<br/>';
echo $b->var3;
echo '<br/>';
// Results
test<br/>get magic methodtest2<br/>test3<br/>destruct of B magic called at end of scriptdestruct of A magic called at end of script
echo $b->var4;
// Results
get magic method<br />
<b>Warning</b>: Undefined property: B::$var4 in <b>[...][...]</b> on line <b>19</b><br />
<br/>
Other MAGIC method:
https://code.tutsplus.com/tutorials/php-magic-methods-cheat-sheet--cms-34681
No comments:
Post a Comment