w JavaScript, mogę związać this
do innej funkcji i wywołać ją z .call
lub .apply
PHP call_user_func wiążą thisArg jak Javascript rozmowy
W PHP mogę zrobić z call_user_func
lub call_user_func_array
, ale jak mogę związać $this
do funkcja?
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:
Dzięki @deceze dla Reflection
pomysł, Znalazłem te rozwiązania, ale nie sądzę, że to dobry dla wydajności (x10 wolniej niż bezpośrednie wywołanie), ale wyraźnie na czytanie.
ja napisałem dwie funkcje:
// 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));
}
i zastąpić call_user_func
do 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
Dlaczego nie dodać argument tej funkcji i przekazać zmienną jako argument ? W przeciwnym razie może ci pomóc słowo kluczowe "use". (http://php.net/functions.anonymous) Ale nie jestem naprawdę pewien. –
Możliwy duplikat http://stackoverflow.com/questions/27014664/php-equivalent-of-javascript-bind – haim770
ponieważ wiem, że jest to rozwiązanie, które przekazuje '$ this' jako argument, po prostu ciekawy czy istnieje jakakolwiek możliwość działania JavaScript – user1542316