通过类链接全局 PHP 函数
是否可以通过一个对象/类链接所有 PHP 函数?
我脑子里有这个,我想象它是这样的:
$c = new Chainer();
$c->strtolower('StackOverFlow')->ucwords(/* the value from the first function argument */)->str_replace('St', 'B', /* the value from the first function argument */);
这应该产生:
Backoverflow
谢谢。
Is it possible to chain all PHP functions through an object/class?
I have this on my mind and I imagine it something like this:
$c = new Chainer();
$c->strtolower('StackOverFlow')->ucwords(/* the value from the first function argument */)->str_replace('St', 'B', /* the value from the first function argument */);
this should produce:
Backoverflow
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看一下:
http://php.net/manual/en/language。 oop5.magic.php
特别是:
http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods
并正确:
http://php.net/manual/en/function.call-user-func-array.php
由于很多人都发布了他们的例子,我也会尝试:
Have a look at:
http://php.net/manual/en/language.oop5.magic.php
especially:
http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods
and propably:
http://php.net/manual/en/function.call-user-func-array.php
As so many posted their examples, I'll try too:
您的意思是
str_replace('St', 'B', ucwords(strtolower('StackOverFlow')))
吗?您上面调用的方法是函数,而不是与任何类相关的方法。 Chainer 必须实现这些方法。如果这就是您想要做的(也许出于不同的目的,这只是一个示例),您的 Chainer 实现可能如下所示:
这在上面的示例中有些作用,但您会调用它是这样的:
请注意,您永远不会从链中返回
/* 第一个函数参数的值 */
的值,因为这是没有意义的。也许你可以用一个全局变量来做到这一点,但这将是非常可怕的。关键是,您可以通过每次返回
$this
来链接方法。对返回值调用 next 方法,该值是同一个对象,因为您返回了它(返回$this
)。了解哪些方法启动和停止链非常重要。我认为这种实现最有意义:
Do you mean to do
str_replace('St', 'B', ucwords(strtolower('StackOverFlow')))
?The methods you are calling above are functions, not methods tied to any class.
Chainer
would have to implement these methods. If this is what you want to do (perhaps for a different purpose and this is just an example) your implementation ofChainer
might look like this:This would work in your above example somewhat, but you would call it like this:
Note that you never get the value of
/* the value from the first function argument */
back out from the chain as this wouldn't make sense. Maybe you could do it with a global variable, but that would be quite hideous.The point is, you can chain methods by returning
$this
each time. The next method is called on the returned value which is the same object because you returned it (returned$this
). It is important to know which methods start and stop the chain.I think that this implementation makes the most sense:
你可以这样做,只要
Chainer
类看起来像“看起来对我来说很愚蠢”。
您也许可以合并一个神奇的 __call 方法,但是处理存储的变量和可选方法参数将是一个很大的痛苦。
You can do this provided the
Chainer
class looks something likeLooks pretty silly to me though.
You might be able to incorporate a magic
__call
method but it's going to be a major pain to deal with the stored variable and optional method arguments.