01 Magic method __get application

<? phpclass Student
{
	// Private member attributes
	private $name = "Zhang";
	private $age  = 24;
	// When accessing inaccessible properties, the __get() magic method is called automatically
	public function __get($n)
	{
		return$this->$n; }}// Create a student object
$obj = new Student();
// Access private attributes
echo {$obj->name} is {$obj->age} years old!;
Copy the code

02 Magic method __set application

<? PHP class Student {private $name = "$name "; private $age = 24; Public function __set($n,$v) {$this->$n = $v; $obj = new Student(); $obj->name = "$obj->name "; $obj->age = 29; var_dump($obj);Copy the code

03 Magic method __isset application

<? PHP class Student {private $name = "$name "; private $age = 24; Public function __isset($n) {return isset($this->$n); $obj = new Student(); If (isset($obj->name)) {echo "yes "; }else {echo "not found or not accessible "; }Copy the code

04 Magic method __unset application

<? PHP class Student {private $name = "$name "; private $age = 24; Public function __unset($n) {unset($this->$n); $obj = new Student(); Unset ($obj->name); var_dump($obj);Copy the code

05 Magic method __call application

<? PHP class Student {private $name = "$name "; private $age = 24; // Magic method __call() is automatically called when accessing nonexistent or inaccessible methods: Public function __call($func,$args) {echo "method {$func}(".implode(',',$args).") does not exist or is not accessible. ; $obj = new Student(); $obj->showInfo($obj->showInfo);Copy the code

06 Magic method __callStatic application

<? PHP class Student {private $name = "$name "; private $age = 24; // The magic method __callStatic() is automatically called when a method does not exist or cannot be accessed statically: $func is the name of the method that was passed in, Public static function __callStatic($func,$args) {echo "method {$func}(".implode(',',$args).") does not exist. ; }} // Call the showInfo() method with the static method Student::showInfo(' Joe ',24,' boy ');Copy the code