用户
 找回密码
 入住 CI 中国社区
搜索
查看: 6127|回复: 6
收起左侧

CI中一段关于魔术函数的代码

[复制链接]
发表于 2009-7-13 22:43:52 | 显示全部楼层 |阅读模式
CI中Mode的构造函数
        function Model()
        {
                // If the magic __get() or __set() methods are used in a Model references can't be used.
                $this->_assign_libraries( (method_exists($this, '__get') OR method_exists($this, '__set')) ? FALSE : TRUE );
               
                // We don't want to assign the model object to itself when using the
                // assign_libraries function below so we'll grab the name of the model parent
                $this->_parent_name = ucfirst(get_class($this));
               
                log_message('debug', "Model Class Initialized");
        }

该句话判断__get和__set函数的存在有何意义?
$this->_assign_libraries( (method_exists($this, '__get') OR method_exists($this, '__set')) ? FALSE : TRUE )
发表于 2009-7-13 23:34:59 | 显示全部楼层
有 __get 和没有 __get _assign_libraries 方法的执行逻辑不同,你看一下后面的代码。
 楼主| 发表于 2009-7-14 00:05:16 | 显示全部楼层
嗯,我看了,为啥要不同啊,__get和需不需要用引用由关系吗?
发表于 2009-7-14 00:58:28 | 显示全部楼层
以下摘自system/libraries/Model.php
// In some cases using references can cause
// problems so we'll conditionally use them
if ($use_reference == TRUE)
{
        $this->$key = NULL; // Needed to prevent reference errors with some configurations
        $this->$key =& $CI->$key;
}
else
{
        $this->$key = $CI->$key;
}
use 金山糍粑+金山快意 to understand Rick大叔的意思,发挥主观能动性
 楼主| 发表于 2009-7-15 00:09:23 | 显示全部楼层
字面的意思是说,有些引用的情况会导致问题,但是这和魔术函数有什么关系?
发表于 2009-7-15 00:15:29 | 显示全部楼层
呵呵,说实话我没看出来有什么关系。
发表于 2009-7-15 04:17:48 | 显示全部楼层
__set __get是PHP5新加的东西,PHP5“放弃”了PHP4对于变量引用的一种写法:
$this->foo(&$myValue); 这在PHP5会有warning提示让你不要这么写,
我以前用过的PHP5某个版本遇到此写法会再给你甩一个error,停止执行

当你的model存在__set方法的时候,如果使用$this->$key = &$CI->$key;
PHP5会执行一个$this->__set($key, &$CI->$key),然后抛出warning/error

这就是Rick大叔注释里说的referrence error

评分

参与人数 1威望 +3 收起 理由
Hex + 3 学习了!

查看全部评分

本版积分规则