PHP Magic Law __set and __get

Source: Internet
Author: User

Set andGet is a common 2 magic method in object-oriented.
? set: When assigning a value to an inaccessible property,Set () is called. The syntax is as follows:

public void __set ( string $name , mixed $value )

? Get: Gets () is called when the value of an inaccessible property is read. The syntax is as follows:

public mixed __get ( string $name )

Description: The parameter $name refers to the name of the variable to be manipulated. The $value parameter of the __set () method specifies the value of the $name variable.
The sample code is as follows:

<?phpclass sportobject{//class sportobjectprivate $type = '; Private variable $typepublic function __get ($name) {//Declares Magic Method __get () echo "Tune        Use the __get method \ n "; if (Isset ($this-$name)) {//Determine if the variable is declared echo ' variable '. $name. ' The value is: '. $this-$name. '        <br> '; }else{echo ' variable '. $name. '             Undefined, initialized to 0<br> ';                             $this-$name = 0; If not declared, initialize the variable}} public function __set ($name, $value) {//Declare Magic Method __set () echo        "Call __set method \ n";            if (Isset ($this, $name)) {//Determine if the variable is defined $this, $name = $value; echo ' variable '. $name. ' The assignment is: '. $value. '        <br> ';                            }else{$this $name = $value; If undefined, the variable is continued to be assigned the echo ' variable '. $name. ' was initialized to: '. $value. '     <br> '; Output warningMessage}} public function test () {echo $this->type;                                }} $MyComputer = new Sportobject ();                                    Instantiated Object $mycomputer$mycomputer-type = ' DIY ';                                        Assign a value $MyComputer-type to a variable;                                        Call variable $type$mycomputer CPU; Call Variable $name?>

Code parsing:

    1. Call $MyComputer-type = ' DIY '; Assigning a value to the Type property, because the Type property is private and cannot be accessed outside the class, calls the set () Magic method, first judging the isset ($this-$name) in the IF statement, according to set ($name, $ Value), we know that the value of $name is type, the value of $value is Diy,isset ($this, $name) is Isset ($this->type), the previous reference to type this property is private, But inside the class is callable, so if statement is set up, execute the following code:

$this $name = $value;
echo ' variable '. $name. ' The assignment is: '. $value. ' <br> ';

2.  调用$MyComputer->type, 是读取type属性,同理,执行__get($name)方法。首先判断isset($this->$name),if语句成立,执行下面代码:

echo ' variable '. $name. ' The value is: '. $this-$name. ' <br> ';
Since the previous assignment for $this->type is DIY, it is output directly.

3.  调用$MyComputer -> cpu; 读取cpu这个不存在属性,调用__get($name)方法,首先判断if语句isset($this->$name),由于没有这个cup属性,所以执行else语句内容,代码如下:

echo ' variable '. $name. ' Undefined, initialized to 0<br> ';
$this-$name = 0;

在代码最后,调用了$this->$name = 0; 即为不存在的属性赋值,所以会再次调用__set($name)方法。同样是判断if语句,这时if语句为false,执行else中的代码如下:

$this $name = $value;
echo ' variable '. $name. ' was initialized to: '. $value. ' <br> ';

PHP Magic Law __set and __get

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.