• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 4年前 (2022-01-23) 16次浏览 已收录 0个评论
文章目录[隐藏]

在通过__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 "</br>Ba<i>1本文来#源gaodai$ma#com搞$代*码*网</i><pre>搞代gaodaima码

sePerson __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); }

}

回复内容:

在通过__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 "</br>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 "</br>name:".($this->name)."</br>age:".($this->age)."</br>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;


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:$this->$propertyName = $value,$this->propertyName = $value的区别?

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址