PHP 方法链接
所以我想知道当初始方法是静态函数时是否有一种方法链的方法。这就是我的意思:
class foo
{
public static function a()
{
$foo = new foo;
return $foo->bar();
}
public function bar()
{
return $this;
}
public function b()
{
return 1;
}
}
print foo::a()->b();
编辑 print foo::a()->b();
不是 print foo:a()->b();
So i was wondering if there is a way to method chain, when the initial method is a static function. Here is what I mean:
class foo
{
public static function a()
{
$foo = new foo;
return $foo->bar();
}
public function bar()
{
return $this;
}
public function b()
{
return 1;
}
}
print foo::a()->b();
EDITprint foo::a()->b();
not print foo:a()->b();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
静态方法或其他方法,只要该方法返回一个对象,无论是自身还是其他方法,这些方法都可以链接起来,与您正在尝试的方法相同。
Static Methods or Other Methods, as long as the method is returning an object either self or some other, the methods can be chained, with the same method you are attempting.
这行
应该是
,并且您将无法在静态方法中返回
$this
它需要首先实例化:
this line
should be
and you will not be able to return
$this
in a static methodit needs to be instantiated first:
只是一种答案,而且有些特殊:
但我建议您让您的对象附带一个工厂过程:
这可能会消除您的一些困惑。通过避免静态和对象方法调用的混合,它甚至看起来更好一点:
它基本上外部化了静态函数。并且您的对象仅实现返回 $this 或实际结果的可链接方法。
Only sort of an answer, and somewhat idiosyncratic:
But I would advise that you have your object accompanied by a factory procedure instead:
This might remove some of the confusion for you. And it even looks a bit nicer by avoiding the mix of static and object method calls:
It basically externalizes the static function. And your object only implements the chainable methods which return $this, or actual results.