Php Magic Method __get, __set use instance, __get__set
__get ()-__get () is called when the value of the Inaccessible property is read.
__set ()-__set () is called when a value is assigned to an inaccessible property.
Copy the Code code as follows:
<?php
/**
* Clear understanding of __get () __set ()
*/
Class Example {
Properties of the public
Public $public = ' pub ';
Protected-This attribute is available in subclasses
protected $protected = ' Pro ';
Private-this property can only be used by this class
Private $private = ' pri ';
The __get () method is loaded automatically when a property in the Access object does not exist or is not a public property
Public Function __get ($name) {
Return ' Call the __get () method: '. $name;
}
When assigning a value to an attribute of an object, the __set () method is loaded automatically if the property does not exist or is not a public property
Public Function __set ($name, $value) {
echo "\nname:". $name. ', Value: '. $value. " \ n ";
}
}
$example = new example;
Echo '
';
echo $example->public. " \ n ";
echo $example->protected. " \ n ";
echo $example->private. " \ n ";
echo $example->other. " \ n ";
Echo ';
$example->public = ' lic '; This assignment succeeds all not showing
$example->protected = ' tec ';
$example->private = ' VAT ';
$example->other = ' er ';
Echo '
';
Echo ' Print public properties: '. $example->public;
The results are as follows:
Copy the Code code as follows:
Pub
Call the __get () method: protected
Call the __get () method: Private
Call the __get () method: Other
Name:protected,value:tec
Name:private,value:vat
Name:other,value:er
Print Public Property: Lic
http://www.bkjia.com/PHPjc/917032.html www.bkjia.com true http://www.bkjia.com/PHPjc/917032.html techarticle php Magic Method __get, __set use instance, __get__set __get ()-When reading the value of an inaccessible property, __get () is called. __set ()-When assigning a value to an inaccessible property, __set () is ...