将 $this 传递给函数

jol*_*olt 5 php oop

在 Javascript 中有call()and apply(),它部分地存在,但在 PHP 中解析为call_user_func()and 。call_user_func_array()

现在,这里的区别在于我们可以使用call()和传递一个变量,以便在函数作用域内apply()使用。this

我可以用 PHP 实现类似的功能吗?

更新:

在 JavaScript 中:

var x = function(passed)
{
    return { dis : this, passd : passed };
};

console.log(x(44)); // window, 44

console.log(x.call(25, 44)); // 25, 44
Run Code Online (Sandbox Code Playgroud)

.call()函数作用域内的第一个参数变为this

lor*_*o-s 3

来自PHP回调手册:

实例化对象的方法作为数组传递,该数组包含索引 0 处的对象和索引 1 处的方法名称。

下面的例子:

// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj, 'myCallbackMethod'));
Run Code Online (Sandbox Code Playgroud)