Home >Backend Development >PHP Tutorial >【ThinkPHP】诡谲的现象,$this->rowUser['ext'] = ''无法赋值!但是array_merge 却可以

【ThinkPHP】诡谲的现象,$this->rowUser['ext'] = ''无法赋值!但是array_merge 却可以

WBOY
WBOYOriginal
2016-06-13 11:59:481006browse

【ThinkPHP】诡异的现象,$this->rowUser['ext'] = '';无法赋值!但是array_merge 却可以!
遇到一个非常匪夷所思的事情,在thinkphp3.1.3里面。

<br /><?php<br /><br />/**<br /> * Class UserAction by <[email&#160;protected]><br /> */<br />class UserAction extends InitAction<br />{<br /><br />    public function index()<br />    {<br />        $where = array(<br />            'mid' => array('eq', $this->getUid())<br />        );<br />        $this->rowUser = M('Member')->where($where)->find();<br /><br />        //测试1<br />        $this->rowUser['ext'] = '名字4';<br />        dump($this->rowUser); //里面没有ext这个键!<br /><br />        //测试2 有<br />        $arr['ext'] = '名字4';<br />        $this->rowUser = array_merge($arr, $this->rowUser);<br />        dump($this->rowUser); //里面有ext这个了<br /><br />        //测试3 <br />        $this->rowUser2 = array(<br />            'a' => 'A',<br />            'b' => 'B',<br />        );<br />        $this->rowUser2['ext'] = '3333';<br />        dump($this->rowUser2); //里面也没有ext这个<br />        die;<br />    }<br />}<br />


这个是怎么回事,很奇怪!


------解决方案--------------------
和访问该类的方法一样$this->方法名 时该类或者继承的类中必须有该方法。
------解决方案--------------------
如果你的 UserAction 或其父类中定义有 __set、__get 方法,出现这种情况是很正常的
因为他们可能不按常理出牌
------解决方案--------------------
同样碰到过这个问题
TP在控制器中赋值,其实走的__set方法,被赋值到视图类View的tVar变量中了
然后我发现__set魔术方法有个BUG,拦截不到直接对数组属性的某个元素赋值的操作:
$this->arr['a'] = 1; 这样在TP中是直接对$this->arr赋值,而我们读取其实都是从$this->view->tVar

Statement:
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