In Javascript, i can bind this
to another function and invoke it with .call
or .apply
In PHP, i can do that with call_user_func
or call_user_func_array
, but how can i bind $this
into the function?
Javascript:
function greet() {
alert('Hello ' + this.name);
}
function SomeClass() {}
SomeClass.prototype = {
name: 'John',
test: function() {
greet.call(this);
}
}
var me = new SomeClass();
me.test(); // Hello John
PHP:
function greet() {
echo 'Hello ' . $this->name;
}
class SomeClass {
public $name = 'John';
function test() {
call_user_func('greet');
}
}
$me = new SomeClass;
$me->test(); // Fatal error: Using $this when not in object context
UPDATE:
Thanks @deceze for the Reflection
idea, I've found these solutions but I don't think it's good for performance (x10 slower than directly call), but much clearly on reading.
I've wrote two functions:
// See also Javascript: Function.prototype.apply()
function function_apply($fn, $thisArg, $argsArray = array()) {
static $registry;
if (is_string($fn)) {
if (!isset($registry[$fn])) {
$ref = new \ReflectionFunction($fn);
$registry[$fn] = $ref->getClosure();
}
$fn = $registry[$fn];
}
return call_user_func_array($fn->bindTo($thisArg), $argsArray);
}
// See also Javascript: Function.prototype.call()
function function_call($fn, $thisArg /*, arg1, arg2 ... */) {
return function_apply($fn, $thisArg, array_slice(func_get_args(), 2));
}
and replace call_user_func
to function_call
:
function greet() {
echo 'Hello ' . $this->name;
}
class SomeClass {
public $name = 'John';
function test() {
function_call('greet', $this);
}
}
$me = new SomeClass;
$me->test(); // Hello John