pro=$v; echo "$vn"; }}$a=new aa;$a->b=1;输出是a a 1 1;2个问题,为何"/> pro=$v; echo "$vn"; }}$a=new aa;$a->b=1;输出是a a 1 1;2个问题,为何">

关于_set的有关问题

WBOY
Release: 2016-06-13 12:09:08
Original
779 people have browsed it

关于__set的问题

<br />class aa{<br />	function __set($pro, $v){<br />		echo "a\n";<br />		$this->pro=$v;<br />		echo "$v\n";<br />	}<br />}<br /><br />$a=new aa;<br />$a->b=1;<br />
Copy after login


输出是a a 1 1;

2个问题,为何输出2次;

为何是a a 1 1,而不是a 1 a 1;


------解决思路----------------------
你把 echo "a\n"; 改成 echo "$pro\n"; 就会看到
b     因为 b 属性不存在,所以进入了 __set 方法
pro 同样因为 pro 属性不存在,再次进入 __set 方法
1    进入两次,打印两次值就不足为奇了
1

当 $this->pro=$v;
改为 $this->$pro=$v;
就只进入一次了

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template