$ This-> $ propertyName = $ value, $ this-> What is the difference between propertyName = $ value?

Source: Internet
Author: User
When setting private member attributes through _ set ($ propertyName, $ value), use $ this-& amp; gt; $ propertyName $ value can be used to modify the private variable value. The $ this-& amp; gt; propertyName $ value write rule cannot be used to modify the variable value. Complete... when setting private member attributes through _ set ($ propertyName, $ value,
You can use $ this-> $ propertyName = $ value to modify the private variable value,
You cannot modify the variable value by using the write rules $ this-> propertyName = $ value.

Complete code:

Class BasePerson {

   private $name;   private $age;   private $average;   function __construct(){       $num_args = func_num_args();       if($num_args == 1){           $this->name = func_get_arg(0);       } elseif($num_args == 2){           $this->name = func_get_arg(0);           $this->age = func_get_arg(1);       } elseif($num_args == 3){           $this->name = func_get_arg(0);           $this->age = func_get_arg(1);           $this->average = func_get_arg(2);       }   }      private function __set($propertyName,$propertyValue){       echo "
BasePerson __set()"; if($propertyName == "name"){ if($propertyValue == "zhangsan" || $propertyValue == "wangwu"){ $this->$propertyName = $propertyValue; } } elseif($propertyName=="age"){ if($propertyValue >150 || $propertyValue <0){ return ; } } $this->$propertyName = $propertyValue; } function __toString(){ return "
name:".($this->name)."
age:".($this->age)."
average".($this->average); }

}

Reply content:

When the private member attribute is set through _ set ($ propertyName, $ value,
You can use $ this-> $ propertyName = $ value to modify the private variable value,
You cannot modify the variable value by using the write rules $ this-> propertyName = $ value.

Complete code:

Class BasePerson {

   private $name;   private $age;   private $average;   function __construct(){       $num_args = func_num_args();       if($num_args == 1){           $this->name = func_get_arg(0);       } elseif($num_args == 2){           $this->name = func_get_arg(0);           $this->age = func_get_arg(1);       } elseif($num_args == 3){           $this->name = func_get_arg(0);           $this->age = func_get_arg(1);           $this->average = func_get_arg(2);       }   }      private function __set($propertyName,$propertyValue){       echo "
BasePerson __set()"; if($propertyName == "name"){ if($propertyValue == "zhangsan" || $propertyValue == "wangwu"){ $this->$propertyName = $propertyValue; } } elseif($propertyName=="age"){ if($propertyValue >150 || $propertyValue <0){ return ; } } $this->$propertyName = $propertyValue; } function __toString(){ return "
name:".($this->name)."
age:".($this->age)."
average".($this->average); }

}

$ Name = 'test'; $ name = '000000'; var_dump ($ test ); // output 123 // hope you can understand // similarly $ this-> $ proertyName // you need to check the value of $ proertyName, is to set the value of $ proertyName. this attribute $ proertyName = 'age'; $ this-> $ proertyName = '12'; var_dump ($ this-> age ); // output 12 $ this-> proertyName // set the property of proertyName.

Landlord,$this->$propertyName = $valueThis assignment is$thisThe variable in the instance is$propertyNameVariable value.$propertyNameIt is still a variable, and$this->propertyNameIs the $ this attribute;

The former is like this, for example$propertyName = 'enable';
So$this->$propertyNameIt is equivalent$this->enable;

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.