2015-10-20 21:48:42 2555次浏览 1条回答 0 悬赏 20 金钱

想請教

class model {
    public $que ;
}

$model = new model();
$model['que'] = 'xxx';               // not working 型別不對
$model->que = 'abc';

但是 yii 的 $model = new $model();
$model['que'] = 'xxx';               // 卻可以 到底怎麼做到的!!

有人可以幫我解惑嗎~~ yii 他是怎麼處理的

最佳答案

  • 性感的农民 发布于 2015-10-21 00:29 举报

    The tricker is ArrayAccess.

    class Model extends Component implements IteratorAggregate, ArrayAccess, Arrayable
    {
    

    You can try the following:

    <?php
    class model implements ArrayAccess
    {
    	public $que ;
    	 //This method is required by the SPL interface [[\ArrayAccess]].
    	public function offsetExists($offset)
    	{
    		return $this->$offset !== null;
    	}
    	 //This method is required by the SPL interface [[\ArrayAccess]].
    	public function offsetGet($offset)
    	{
    		return $this->$offset;
    	}
    	 //This method is required by the SPL interface [[\ArrayAccess]].
    	public function offsetSet($offset, $item)
    	{
    		$this->$offset = $item;
    	}
    	 //This method is required by the SPL interface [[\ArrayAccess]].
    	public function offsetUnset($offset)
    	{
    		$this->$offset = null;
    	}
    }
    
    $model = new model();
    $model['que'] = 'xxx';               // <================   it works now%
    
    1 条回复
    回复于 2015-10-21 10:10 回复

    非常感謝 我去研究一下 SPL!!

    觉得很赞
    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
bryson
副总裁

bryson Taipei

注册时间:2015-07-22
最后登录:2017-04-03
在线时长:84小时54分
  • 粉丝19
  • 金钱4885
  • 威望230
  • 积分8025

热门问题