是否可以在变量中存储类方法的主体?(Is it possible to store body of class method in a variable?)

如果我有这样的课程:

class MyClass { protected function method1() { // Method body } }

我可以以某种方式在变量中保存此方法的主体,所以我可以将它传递给应用程序吗?

例如这样:

class MyClass { function __construct() { $var = // body of method1 $something = new AnotherClass($var); } protected function method1($arg1, $arg2) { // Method body } } class AnotherClass { function __construct($var) { $var($this->arg1, $this->arg2); } }

我这样的事可能吗?

If I have a this kind of class:

class MyClass { protected function method1() { // Method body } }

Can I somehow save body of this method in variable, so I could pass it down the application?

For example like this:

class MyClass { function __construct() { $var = // body of method1 $something = new AnotherClass($var); } protected function method1($arg1, $arg2) { // Method body } } class AnotherClass { function __construct($var) { $var($this->arg1, $this->arg2); } }

I something like this possible?

最满意答案

您无法传递正文 ,但可以将callable引用传递给该函数:

... new AnotherClass(array($this, 'method1')) ... class AnotherClass { function __construct(callable $var) { $var($this->arg1, $this->arg2); } }

在这种情况下,该方法protected ,因此AnotherClass无法直接调用它。 您可以使用匿名函数:

... new AnotherClass(function ($arg1, $arg2) { return $this->method1($arg1, $arg2); }) ...

匿名函数中的callable类型提示和$this仅适用于PHP 5.4,并且匿名函数仅在5.3+中可用。 对于任何以前的版本,解决方法或多或少都相当复杂。 我怀疑这是多么好的解决方案,其他设计模式在这里可能更合适。

You can't pass the body, but you can pass a callable reference to that function:

... new AnotherClass(array($this, 'method1')) ... class AnotherClass { function __construct(callable $var) { $var($this->arg1, $this->arg2); } }

In this case the method is protected, so AnotherClass can't call it directly. You can use anonymous functions then:

... new AnotherClass(function ($arg1, $arg2) { return $this->method1($arg1, $arg2); }) ...

The callable type hint and $this in anonymous functions only works since PHP 5.4 and anonymous functions are only available in 5.3+. For any previous versions the workaround would be more or less pretty complex. I doubt how good a solution this really is though, other design patterns may be a lot more appropriate here.

是否可以在变量中存储类方法的主体?(Is it possible to store body of class method in a variable?)

如果我有这样的课程:

class MyClass { protected function method1() { // Method body } }

我可以以某种方式在变量中保存此方法的主体,所以我可以将它传递给应用程序吗?

例如这样:

class MyClass { function __construct() { $var = // body of method1 $something = new AnotherClass($var); } protected function method1($arg1, $arg2) { // Method body } } class AnotherClass { function __construct($var) { $var($this->arg1, $this->arg2); } }

我这样的事可能吗?

If I have a this kind of class:

class MyClass { protected function method1() { // Method body } }

Can I somehow save body of this method in variable, so I could pass it down the application?

For example like this:

class MyClass { function __construct() { $var = // body of method1 $something = new AnotherClass($var); } protected function method1($arg1, $arg2) { // Method body } } class AnotherClass { function __construct($var) { $var($this->arg1, $this->arg2); } }

I something like this possible?

最满意答案

您无法传递正文 ,但可以将callable引用传递给该函数:

... new AnotherClass(array($this, 'method1')) ... class AnotherClass { function __construct(callable $var) { $var($this->arg1, $this->arg2); } }

在这种情况下,该方法protected ,因此AnotherClass无法直接调用它。 您可以使用匿名函数:

... new AnotherClass(function ($arg1, $arg2) { return $this->method1($arg1, $arg2); }) ...

匿名函数中的callable类型提示和$this仅适用于PHP 5.4,并且匿名函数仅在5.3+中可用。 对于任何以前的版本,解决方法或多或少都相当复杂。 我怀疑这是多么好的解决方案,其他设计模式在这里可能更合适。

You can't pass the body, but you can pass a callable reference to that function:

... new AnotherClass(array($this, 'method1')) ... class AnotherClass { function __construct(callable $var) { $var($this->arg1, $this->arg2); } }

In this case the method is protected, so AnotherClass can't call it directly. You can use anonymous functions then:

... new AnotherClass(function ($arg1, $arg2) { return $this->method1($arg1, $arg2); }) ...

The callable type hint and $this in anonymous functions only works since PHP 5.4 and anonymous functions are only available in 5.3+. For any previous versions the workaround would be more or less pretty complex. I doubt how good a solution this really is though, other design patterns may be a lot more appropriate here.