Home  >  Article  >  Backend Development  >  关于call_user_func_array 跟 引用 的一段代码 不懂求解释

关于call_user_func_array 跟 引用 的一段代码 不懂求解释

WBOY
WBOYOriginal
2016-06-13 12:51:43727browse

关于call_user_func_array 和 引用 的一段代码 不懂求解释

class PbObject{

var $params;
var $fontFace = 'incite.ttf';

function Object() {
$args = func_get_args();
if (method_exists($this, '__destruct')) {
register_shutdown_function (array(&$this, '__destruct'));
}
call_user_func_array(array(&$this, '__construct'), $args);
}

function __construct(){}

function toString() {
$class = get_class($this);
return $class;
}
}



请问这段代码中的  call_user_func_array(array(&$this, '__construct'), $args);  为什么$this前面加引用符&?  这样的代码我一直没有搞懂过。  不加&不也是一样能调用本身的 __construct 方法?  加了有什么作用? 

引用符?类对象 引用符 对象
------解决方案--------------------
当 call_user_func_array 调用的函数是类的方法时,需要用数组同时传递 类(对象)和 方法名
你的 $this 就是对象了
在 php4 中,对象默认以值传递,如果没有引用声明(&),那么实际上是在另一个对象上执行的,如果方法中修改了类属性,那么这一修改将不能作用到调用者本身
在 php5 中,对象都是以引用传递,所以就没有必要冠以 & 了

你的代码中用 var 声明类属性,这显然是 php4 风格的
php4 的类没有析构函数,所以代码用 register_shutdown_function 注册了一个
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