无法从类中调用静态方法作为变量名?

发布于 2024-09-24 15:43:37 字数 1232 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

焚却相思 2024-10-01 15:43:37

是的。该语法是在 5.3 中引入的。

要解决 <= 5.2 的问题,您可以使用 call_user_func:

call_user_func(array($className, $funcName), $arg1, $arg2, $arg3);

call_user_func_array:

call_user_func_array(array($className, $funcName), array($arg1, $arg2, $arg3));

但另一方面,您想要执行的操作并不'真的没有道理...

为什么将它作为静态函数?无论如何,implementation 中的构造函数都需要一个对象(这就是 strategyInterface $strategy 正在寻找的对象)。传递字符串不起作用,因为字符串不实现接口。所以我要做的就是使接口成为非静态的,然后执行以下操作:

$strategy = new StrategyB();
$implementation = new Implementation($strategy);

然后,在构造函数中:

$strFormat = $strategy->getFormatString();

或者,如果您确实仍然希望该方法是静态的,您可以这样做:

$strFormat = call_user_func(array(get_class($strategy), 'getFormatString'));

哦,还有 = & ; new synax 已弃用(并且不会按照您的想法执行无论如何,它确实如此)。

Yes. That syntax was introduced in 5.3

To workaround for <= 5.2, you can use call_user_func:

call_user_func(array($className, $funcName), $arg1, $arg2, $arg3);

or call_user_func_array:

call_user_func_array(array($className, $funcName), array($arg1, $arg2, $arg3));

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 what strategyInterface $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:

$strategy = new StrategyB();
$implementation = new Implementation($strategy);

Then, in the constructor:

$strFormat = $strategy->getFormatString();

Or, if you really still want that method to be static you could do:

$strFormat = call_user_func(array(get_class($strategy), 'getFormatString'));

Oh, and = & new synax is deprecated (and doesn't do what you think it does anyway).

陈独秀 2024-10-01 15:43:37

类型提示会给你带来一些问题:

参数 1 传递给
实现::__construct() 必须
实现接口策略接口,
给出的字符串

Type hinting is going to give you some problems:

Argument 1 passed to
implementation::__construct() must
implement interface strategyInterface,
string given

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文