首页 > 后端开发 > php教程 > $this->$propertyName = $value,$this->propertyName = $value的区别?

$this->$propertyName = $value,$this->propertyName = $value的区别?

WBOY
发布: 2016-07-06 13:53:49
原创
1213 人浏览过

在通过__set($propertyName,$value)设置private成员属性时,
用$this->$propertyName = $value的写法才可以进行private变量值的修改,
采用$this->propertyName = $value的写法则不能进行变量值的修改。

完整代码:

class BasePerson{

<code>   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 $propertyName = $propertyValue;
   }
   
   function __toString(){
       return "name:".($this->name)."age:".($this->age)."average".($this->average);
   }
</code>
登录后复制
登录后复制

}

回复内容:

在通过__set($propertyName,$value)设置private成员属性时,
用$this->$propertyName = $value的写法才可以进行private变量值的修改,
采用$this->propertyName = $value的写法则不能进行变量值的修改。

完整代码:

class BasePerson{

<code>   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 $propertyName = $propertyValue;
   }
   
   function __toString(){
       return "name:".($this->name)."age:".($this->age)."average".($this->average);
   }
</code>
登录后复制
登录后复制

}

<code>$name = 'test';
$$name = '123';
var_dump($test);  //输出123
//希望你能理解

//同理
$this->$proertyName //需要看$proertyName的值, 是设置$proertyName的值这个属性
$proertyName = 'age';
$this->$proertyName = '12';
var_dump($this->age);  //输出12

$this->proertyName  //设置的是proertyName这个属性</code>
登录后复制

楼主,$this->$propertyName = $value 这样的赋值是$this实例中变量为$propertyName变量的值,此处的$propertyName仍然是个变量,而$this->propertyName就是$this的属性了;

前者的情况是这样的,例如$propertyName = 'enable';
那么$this->$propertyName 就等同与 $this->enable;

相关标签:
php
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板