无法从类中调用静态方法作为变量名?
我正在使用 php 5.2.6。我有一个策略模式,并且策略有一个静态方法。在实际实现策略之一的类中,它获取要实例化的策略类的名称。但是,我想在实例化之前调用其中一个静态方法,如下所示:
$strNameOfStrategyClass::staticMethod();
但它给出了 T_PAAMAYIM_NEKUDOTAYIM
。
$> cat test.php
<?
interface strategyInterface {
public function execute();
public function getLog();
public static function getFormatString();
}
class strategyA implements strategyInterface {
public function execute() {}
public function getLog() {}
public static function getFormatString() {}
}
class strategyB implements strategyInterface {
public function execute() {}
public function getLog() {}
public static function getFormatString() {}
}
class implementation {
public function __construct( strategyInterface $strategy ) {
$strFormat = $strategy::getFormatString();
}
}
$objImplementation = & new implementation("strategyB") ;
$> php test.php
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in /var/www/test.php on line 24
$> php -v
PHP 5.2.6-1+lenny9 with Suhosin-Patch 0.9.6.2 (cli) (built: Aug 4 2010 03:25:57)
这在 5.3 中可以工作吗?
I'm using php 5.2.6. I have a strategy pattern, and the strategies have a static method. In the class that actually implements one of the strategies, it gets the name of the strategy class to instantiate. However, I wanted to call one of the static methods before instantiation, like this:
$strNameOfStrategyClass::staticMethod();
but it gives T_PAAMAYIM_NEKUDOTAYIM
.
gt; cat test.php
<?
interface strategyInterface {
public function execute();
public function getLog();
public static function getFormatString();
}
class strategyA implements strategyInterface {
public function execute() {}
public function getLog() {}
public static function getFormatString() {}
}
class strategyB implements strategyInterface {
public function execute() {}
public function getLog() {}
public static function getFormatString() {}
}
class implementation {
public function __construct( strategyInterface $strategy ) {
$strFormat = $strategy::getFormatString();
}
}
$objImplementation = & new implementation("strategyB") ;
gt; php test.php
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in /var/www/test.php on line 24
gt; php -v
PHP 5.2.6-1+lenny9 with Suhosin-Patch 0.9.6.2 (cli) (built: Aug 4 2010 03:25:57)
Would this work in 5.3?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。该语法是在 5.3 中引入的。
要解决 <= 5.2 的问题,您可以使用
call_user_func
:或
call_user_func_array
:但另一方面,您想要执行的操作并不'真的没有道理...
为什么将它作为静态函数?无论如何,
implementation
中的构造函数都需要一个对象(这就是strategyInterface $strategy
正在寻找的对象)。传递字符串不起作用,因为字符串不实现接口。所以我要做的就是使接口成为非静态的,然后执行以下操作:然后,在构造函数中:
或者,如果您确实仍然希望该方法是静态的,您可以这样做:
哦,还有
= & ; new
synax 已弃用(并且不会按照您的想法执行无论如何,它确实如此)。Yes. That syntax was introduced in 5.3
To workaround for <= 5.2, you can use
call_user_func
:or
call_user_func_array
:But on another note, what you're trying to do doesn't really make sense...
Why have it as a static function? Your constructor in
implementation
is expecting an object anyway (that's whatstrategyInterface $strategy
is looking for). Passing a string won't work, since strings don't implement interfaces. So what I would do, is make the interface non-static, and then do something like:Then, in the constructor:
Or, if you really still want that method to be static you could do:
Oh, and
= & new
synax is deprecated (and doesn't do what you think it does anyway).类型提示会给你带来一些问题:
Type hinting is going to give you some problems: