I recently saw some sample PHP code that looked like this:
$myObj->propertyOne = 'Foo'
->propertyTwo = 'Bar'
->MethodA('blah');
As opposed to:
$myObj->propertyOne = 'Foo';
$myObj->propertyTwo = 'Bar';
$myObj->MethodA('blah');
Is this from a particular framework or a particular version of PHP because I have never seen it work?
I can't believe that it would actually work as you've shown it with the semi-colons after each line, nor for assigning properties directly; you may well have seen something like
which is commonly called a
fluent interface
ormethod chaining
, where each of the methods returns the instance of the current object viareturn $this
I've had a look at Method Chaining which I'd never heard of in PHP before. Obviously my example is nonsense.
This post makes sense of it for me:
PHP method chaining?
What you saw was
fluent interface
, however your code sample is wrong. To make long story short,fluent setter
should return$this
:Usage: