【发布时间】:2019-09-20 00:26:43
【问题描述】:
我对下面的脚本代码有疑问:
ClassName::method("AnotherClassName@method");
你如何编写这样的代码?
我想用以下方式退货:
return $AnotherClassName->method();
【问题讨论】:
-
你可以通过这个文档here
标签: php class methods coding-style
我对下面的脚本代码有疑问:
ClassName::method("AnotherClassName@method");
你如何编写这样的代码?
我想用以下方式退货:
return $AnotherClassName->method();
【问题讨论】:
标签: php class methods coding-style
您的静态类可能只是call_user_func() 的别名(某种意义上的):
<?php
class ClassHelper
{
public static function get($classmeth)
{
# Replace @ with :: as noted in the call_user_func() documentation
return call_user_func(str_replace('@', '::', $classmeth));
}
}
所以本质上是这样的:
$result = ClassHelper::get("AnotherClassName@someMethod");
相当于:
$Class = new AnotherClassName();
$result = $Class->someMethod();
【讨论】: