function__call($funName, $arguments) { echo"The function you called:" . $funName . "(parameter:" ; // Print the method's name that is not existed. print_r($arguments); // Print the parameter list of the method that is not existed. echo")does not exist!!<br>\n"; } } $Person = newPerson(); $Person->run("teacher"); // If the method which is not existed is called within the object, then the __call() method will be called automatically. $Person->eat("John", "apple"); $Person->say();
显示结果
1 2 3
The function you called: run (parameter: Array([0] => teacher)) does not exist! The function you called: eat (parameter: Array([0] => John[1] => apple)) does not exist! Hello world!
The static method you called: run (parameter: Array([0] => teacher)) does not exist! The static method you called: eat (parameter: Array([0] => John[1] => apple)) does not exist! Hello world!
publicfunction__get($propertyName) { if ($propertyName == "age") { if ($this->age > 30) { return$this->age - 10; } else { return$this->$propertyName; } } else { return$this->$propertyName; } } } $Person = newPerson("John", 60); // Instantiate the object with the Person class and assign initial values to the properties with the constructor. echo"Name:" . $Person->name . "<br>"; // When the private property is accessed, the __get() method will be called automatically,so we can get the property value indirectly. echo"Age:" . $Person->age . "<br>"; // The __get() method is called automatically,and it returns different values according to the object itself.
结果显示如下
1 2
Name: John Age: 50
6. __set()
set ($property,$value) 方法用于设置类的私有属性。分配了未定义的属性后,将触发 set () 方法,并且传递的参数是设置的属性名称和值。
1 The name property is private,the __isset() method is called automatically. 1 The age property is private,the __isset() method is called automatically. 1
/** * @param $content * * @return bool */ publicfunction__unset($content) { echo"It is called automatically when we use the unset() method outside the class.<br>"; echoisset($this->$content); } }
It is called automatically when we use the unset() method outside the class. 1 It is called automatically when we use the unset() method outside the class. 1
/** * @return array */ publicfunction__sleep() { echo"It is called when the serialize() method is called outside the class.<br>"; $this->name = base64_encode($this->name); returnarray('name', 'age'); // It must return a value of which the elements are the name of the properties returned. } }
/** * @return array */ publicfunction__sleep() { echo"It is called when the serialize() method is called outside the class.<br>"; $this->name = base64_encode($this->name); returnarray('name', 'age'); // It must return a value of which the elements are the name of the properties returned. }
/** * __wakeup */ publicfunction__wakeup() { echo"It is called when the unserialize() method is called outside the class.<br>"; $this->name = 2; $this->sex = 'Male'; // There is no need to return an array here. } }
It is called when the serialize() method is called outside the class. string(58) "O:6:"Person":2:{s:4:"name";s:8:"5bCP5piO";s:3:"age";i:25;}" It is called when the unserialize() method is called outside the class. object(Person)#2 (3) { ["sex"]=> string(3) "Male" ["name"]=> int(2) ["age"]=> int(25) }