16 个 PHP 开发者必知必会的魔术方法( 二 )

执行结果如下:
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!__get()当你尝试在外部访问对象的私有属性时,应用程序将抛出异常并结束运行 。我们可以使用 __get 方法解决该问题 。该方法可以获取从对象外部获取私有属性的值 。举例如下
<?phpclass Person{private $name;private $age;function __construct($name="", $age=1){$this->name = $name;$this->age = $age;}public function __get($propertyName){if ($propertyName == "age") {if ($this->age > 30) {return $this->age - 10;} else {return $this->$propertyName;}} else {return $this->$propertyName;}}}$Person = new Person("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.【16 个 PHP 开发者必知必会的魔术方法】结果显示如下
Name: JohnAge: 506. __set()set ($property,$value) 方法用于设置类的私有属性 。分配了未定义的属性后,将触发 set () 方法,并且传递的参数是设置的属性名称和值 。
面是演示代码:
<?phpclass Person{private $name;private $age;public function __construct($name="",$age=25){$this->name = $name;$this->age= $age;}public function __set($property, $value) {if ($property=="age"){if ($value > 150 || $value < 0) {return;}}$this->$property = $value;}public function say(){echo "My name is ".$this->name.",I'm ".$this->age." years old";}}$Person=new Person("John", 25); //请注意,类初始化并为“name”和“age”分配初始值 。$Person->name = "Lili";// "name" 属性值被成功修改 。如果没有__set()方法,程序将报错 。$Person->age = 16; // "age"属性修改成功 。$Person->age = 160; //160是无效值,因此修改失败 。$Person->say();//输出:My name is Lili, I'm 16 years old 。代码运行结果:
My name is Lili, I'm 16 years old7. __isset()在使用__isset () 方法之前,让我先解释一下 isset () 方法的用法 。isset () 方法主要用于确定是否设置了此变量 。
 
如果在对象外部使用 isset () 方法,则有两种情况:
 
如果该参数是公共属性,则可以使用 isset () 方法确定是否设置了该属性 。
如果参数是私有属性,则 isset () 方法将不起作用 。
那么对于私有属性,有什么办法知道它是否被设置了吗?当然,只要在类中定义__isset () 方法,就可以在类外部使用 isset () 方法来确定是否设置了私有属性 。
 
当在未定义或不可访问的属性上调用 isset () 或 empty () 时,将调用__isset () 方法 。下面是一个例子:
<?phpclass Person{public $sex;private $name;private $age;public function __construct($name="",$age=25, $sex='Male'){$this->name = $name;$this->age= $age;$this->sex= $sex;}/*** @param $content** @return bool*/public function __isset($content) {echo "The {$content} property is private,the __isset() method is called automatically.<br>";echoisset($this->$content);}}$person = new Person("John", 25); // Initially assigned.echo isset($person->sex),"<br>";echo isset($person->name),"<br>";echo isset($person->age),"<br>";代码运行结果如下:
1The name property is private,the __isset() method is called automatically.1The age property is private,the __isset() method is called automatically.18. __unset()与 isset () 方法类似,当在未定义或不可访问的属性上调用 unset () 方法时,将调用 unset () 方法 。下面是一个例子:
<?phpclass Person{public $sex;private $name;private $age;public function __construct($name="",$age=25, $sex='Male'){$this->name = $name;$this->age= $age;$this->sex= $sex;}/*** @param $content** @return bool*/public function __unset($content) {echo "It is called automatically when we use the unset() method outside the class.<br>";echoisset($this->$content);}}$person = new Person("John", 25); // Initially assigned.unset($person->sex),"<br>";unset($person->name),"<br>";unset($person->age),"<br>";


推荐阅读