Re: Set object properties inline

From: Date: Wed, 01 Jun 2016 20:20:40 +0000
Subject: Re: Set object properties inline
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-93694@lists.php.net to get a copy of this message
On Wed, Jun 1, 2016 at 7:49 PM, Peter Cowburn <petercowburn@gmail.com> wrote: > > While it's not as concise as your example, anonymous classes can do this > already after a fashion. > > $this->fooMethod( > $arg1, > $arg2, > new class() extends FooParams { > // Constructor because prop3's value isn't a constant expression > function __construct() { > $this->prop1 = '...'; > $this->prop2 = '...'; > $this->prop3 = new class() extends Obj1 { > public $prop1 = '...'; > public $prop2 = '...'; > }; > } > } > ); > > While that does avoid the temporary variable it seems more verbose than the temporary variable was to start with, even more so if you want to use variables from the outer context, since you have to thread them through the constructor. It also seems excessive adding a whole new (anonymous) class to the runtime just to avoid a temporary variable, but maybe that doesn't matter.

« previous php.internals (#93694) next »