“new” and fluent interfaces

Last week, I was with some friends and we were talking about fluent interfaces and I show them something that they didn’t know. I got surprised about it because it was introduced in PHP 5.4.

Code from friends:

$user = new User();
$user
    ->setName('Carlos')
    ->setLastname('Buenosvinos')
;

What they don’t know it can be done:

$user = (new User())
    ->setName('Carlos')
    ->setLastname('Buenosvinos')
;

Silly, but cool, isn’t it?

  • algo parecido estuve probando justo ayer pero en javascript. Nice Tip