CI中一段关于魔术函数的代码
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 ) 有 __get 和没有 __get _assign_libraries 方法的执行逻辑不同,你看一下后面的代码。 嗯,我看了,为啥要不同啊,__get和需不需要用引用由关系吗? 以下摘自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大叔的意思,发挥主观能动性 字面的意思是说,有些引用的情况会导致问题,但是这和魔术函数有什么关系? 呵呵,说实话我没看出来有什么关系。 __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]