How to chain method on a newly created object?
问题 I would like to know whether there\'s a way to chain methods on a newly created object in PHP? Something like: class Foo { public function xyz() { ... return $this; } } $my_foo = new Foo()->xyz(); Anyone know of a way to achieve this? 回答1: In PHP 5.4+, the parser's been modified so you can do something like this (new Foo())->xyz(); Wrap the instantiation in parenthesis, and chain away. Prior to PHP 5.4, when you're using the new Classname(); syntax, you can't chain a method call off the