# Set object properties inline

- Thread: https://externals.io/message/93662
- Original archive: https://news-web.php.net/php.internals/93662
- Started by Jesse Schalken on 2016-06-01T02:15:44+00:00
- Messages: 14
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #93662 — Jesse Schalken — 2016-06-01T02:15:44+00:00

URL: https://externals.io/message/93662#93662

Hi internals,

I often have code dealing with plain old PHP objects with properties and no
methods, either as a substitute for keyword arguments, or to represent a
JSON or YAML document for a web service, configuration file or schemaless
database.

At the moment, instantiating an object and setting public properties
requires a temporary variable for each object in the structure:

$obj1 = new Obj1();
$obj1->prop1 = ...;
$obj1->prop2 = ...;

$params = new FooParams();
$params->prop1 = ..;
$params->prop2 = ...;
$params->prop3 = $obj1;

$this->fooMethod($arg1, $arg2, $params);


For large structures, this gets verbose very quick. There is a good example
of this here
<https://github.com/jesseschalken/fail-whale/blob/72870b37c4c21d19f17324a966344ec476b432a7/src/FailWhale/Introspection.php#L22>
involving
18 unnecessarily variables.

I can remove the local variables by defining setters for all the properties:

$this->fooMethod(

    $arg1,
    $arg2,
    (new FooParams())

        ->setProp1(...)

        ->setProp2(...)

        ->setProp3((new Obj1())

        ->setProp1(...)

        ->setProp2(...))

);


But now for each property I have to spend an extra 3-5 lines of code
defining a setter, which is more code than it saved (unless the class is
used heavily enough).

I could define __construct() taking every property as a parameter, but then
each property has to be mentioned another three times (four times with a
doc comment), and the type twice:

class FooParams {

    public int $prop1;
    // ...


    public function __construct(
        int $prop1
        // ...
    ) {

        $this->prop1 = $prop1;
        // ...

    }

}


and where the object is constructed, it isn't immediately visible what the
meaning of each positional parameter is without some IDE assistance (eg
Ctrl+P in PhpStorm), and only specifying some parameters requires filling
preceding ones with defaults.

I could also define the __call() method to automatically expose setters,
but then IDEs and static analysis can't understand what's going on (it
can't see that those methods exist, what their parameters are and what they
return). @method doc comments on the class help, but that's another line
for every property which I have to manually keep in sync with the real
properties.

It would be great if there was a simple shorthand syntax for setting
properties on an object in-line, without needing to extract a variable:

$this->fooMethod(
    $arg1,
    $arg2,
    new FooParams() {
        prop1 = ...,
        prop2 = ...,
        prop3 = new Obj1() {
            prop1 = ...,
            prop2 = ...,
        },
    }
);


This way the structure can be written directly in the code as an expression
and FooParams and Obj1 remain simple containers for properties.

The grammar might look like (I haven't used bison/yacc before):

expr_without_variable:
        /* ... */
    |   expr '{' inline_set_properties '}'
;

inline_set_properties:
        /* empty */
    |   identifier '=' expr
    |   identifier '=' expr ',' inline_set_properties
;


(Although I think that would conflict with the alternative $var{8} syntax
for array/string offset.)

Has this been explored before? What problems can you foresee (or have been
foreseen) with such a feature?

Thanks

---

## #93666 — Lester Caine — 2016-06-01T09:40:08+00:00

URL: https://externals.io/message/93662#93666 · In reply to: #93662

> For large structures, this gets verbose very quick. There is a good example
> of this here
> <https://github.com/jesseschalken/fail-whale/blob/72870b37c4c21d19f17324a966344ec476b432a7/src/FailWhale/Introspection.php#L22>
> involving
> 18 unnecessarily variables.
> 
> I can remove the local variables by defining setters for all the properties:

It's nice to see a page of code I can actually read and follow without
having to dig out the 'new functions' crib sheets ...

The main reason I recognise the code is it has elements I was working
with carried over from the PHP4 stuff I started with and I'm still at
the alternate solution ...

        $static1               = new Data\StaticVariable;
        $static1->name         = 'variable name';
        $static1->value        = $unknown1;
        $static1->functionName = 'blahFunction';
        $static1->className    = null;

Morphed into simply passing an array to the constructor ...

        $static1         = new Data\StaticVariable( array(
          'name'         => 'variable name',
          'value'        => $unknown1,
          'functionName' => 'blahFunction' ) );

        // className defaults to null

Of cause the short cut at that time was to drop the names and simply
count the variables, but what this replaces is all the complications of
named parameters and allowed passing objects in such as '$unknown1'

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

---

## #93667 — Peter Cowburn — 2016-06-01T09:49:17+00:00

URL: https://externals.io/message/93662#93667 · In reply to: #93662

> Hi internals,
>
> I often have code dealing with plain old PHP objects with properties and no
> methods, either as a substitute for keyword arguments, or to represent a
> JSON or YAML document for a web service, configuration file or schemaless
> database.
>
> At the moment, instantiating an object and setting public properties
> requires a temporary variable for each object in the structure:


> $obj1 = new Obj1();
> $obj1->prop1 = ...;
> $obj1->prop2 = ...;
>
> $params = new FooParams();
> $params->prop1 = ..;
> $params->prop2 = ...;
> $params->prop3 = $obj1;
>
> $this->fooMethod($arg1, $arg2, $params);
>
>
> For large structures, this gets verbose very quick. There is a good example
> of this here
> <
> https://github.com/jesseschalken/fail-whale/blob/72870b37c4c21d19f17324a966344ec476b432a7/src/FailWhale/Introspection.php#L22
> > […]
> involving
> 18 unnecessarily variables.
>
> I can remove the local variables by defining setters for all the
> properties:
>
> $this->fooMethod(
>
>     $arg1,
>     $arg2,
>     (new FooParams())
>
>         ->setProp1(...)
>
>         ->setProp2(...)
>
>         ->setProp3((new Obj1())
>
>         ->setProp1(...)
>
>         ->setProp2(...))
>
> );
>
>
> But now for each property I have to spend an extra 3-5 lines of code
> defining a setter, which is more code than it saved (unless the class is
> used heavily enough).
>
> I could define __construct() taking every property as a parameter, but then
> each property has to be mentioned another three times (four times with a
> doc comment), and the type twice:
>
> class FooParams {
>
>     public int $prop1;
>     // ...
>
>
>     public function __construct(
>         int $prop1
>         // ...
>     ) {
>
>         $this->prop1 = $prop1;
>         // ...
>
>     }
>
> }
>
>
> and where the object is constructed, it isn't immediately visible what the
> meaning of each positional parameter is without some IDE assistance (eg
> Ctrl+P in PhpStorm), and only specifying some parameters requires filling
> preceding ones with defaults.
>
> I could also define the __call() method to automatically expose setters,
> but then IDEs and static analysis can't understand what's going on (it
> can't see that those methods exist, what their parameters are and what they
> return). @method doc comments on the class help, but that's another line
> for every property which I have to manually keep in sync with the real
> properties.
>
> It would be great if there was a simple shorthand syntax for setting
> properties on an object in-line, without needing to extract a variable:
>
> $this->fooMethod(
>     $arg1,
>     $arg2,
>     new FooParams() {
>         prop1 = ...,
>         prop2 = ...,
>         prop3 = new Obj1() {
>             prop1 = ...,
>             prop2 = ...,
>         },
>     }
> );
>

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 = '...';
            };
        }
    }
);


>
>
> This way the structure can be written directly in the code as an expression
> and FooParams and Obj1 remain simple containers for properties.
>
> The grammar might look like (I haven't used bison/yacc before):
>
> expr_without_variable:
>         /* ... */
>     |   expr '{' inline_set_properties '}'
> ;
>
> inline_set_properties:
>         /* empty */
>     |   identifier '=' expr
>     |   identifier '=' expr ',' inline_set_properties
> ;
>
>
> (Although I think that would conflict with the alternative $var{8} syntax
> for array/string offset.)
>
> Has this been explored before? What problems can you foresee (or have been
> foreseen) with such a feature?
>
> Thanks

---

## #93678 — guilhermeblanco@gmail.com — 2016-06-01T14:38:32+00:00

URL: https://externals.io/message/93662#93678 · In reply to: #93667

Hi,

I very much liked the proposal, specially if we do consider a potential
reusability if anyone ever decide to revisit Attributes RFC. It's out of
scope how I'd see this implemented, so let me focus on the proposal only.
There're some edge cases that need to be discussed and considered here.
Here's one quick example:

declare("foo", "bar");

class Foo {
    public $bar;
}

$f = new Foo() {
    foo = "bar"
};

Should "foo" be considered as a property identifier or as a constant, which
would then be converted into "bar" and then used as $f->bar = "bar"?


Regards,


On Wed, Jun 1, 2016 at 5:49 AM, Peter Cowburn <petercowburn@gmail.com>
wrote:

> 
>
> > […]
> no
> > […]
>
>
> > […]
> example
> > […]
> https://github.com/jesseschalken/fail-whale/blob/72870b37c4c21d19f17324a966344ec476b432a7/src/FailWhale/Introspection.php#L22
> > […]
> then
> > […]
> the
> > […]
> they
> > […]
>
> 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 = '...';
>             };
>         }
>     }
> );
>
>
> > […]
> expression
> > […]
> been
> > […]
>



-- 
Guilherme Blanco
Lead Architect at E-Block

---

## #93679 — Matt Fonda — 2016-06-01T15:37:58+00:00

URL: https://externals.io/message/93662#93679 · In reply to: #93662

On Tue, May 31, 2016 at 7:15 PM, Jesse Schalken <me@jesseschalken.com>
wrote:

> Hi internals,
>
> I often have code dealing with plain old PHP objects with properties and no
> methods, either as a substitute for keyword arguments, or to represent a
> JSON or YAML document for a web service, configuration file or schemaless
> database.
>
> At the moment, instantiating an object and setting public properties
> requires a temporary variable for each object in the structure:
>
> $obj1 = new Obj1();
> $obj1->prop1 = ...;
> $obj1->prop2 = ...;
>
> $params = new FooParams();
> $params->prop1 = ..;
> $params->prop2 = ...;
> $params->prop3 = $obj1;
>
> $this->fooMethod($arg1, $arg2, $params);
>
>
> For large structures, this gets verbose very quick. There is a good example
> of this here
> <
> https://github.com/jesseschalken/fail-whale/blob/72870b37c4c21d19f17324a966344ec476b432a7/src/FailWhale/Introspection.php#L22
> > […]
> involving
> 18 unnecessarily variables.
>
> I can remove the local variables by defining setters for all the
> properties:
>
> $this->fooMethod(
>
>     $arg1,
>     $arg2,
>     (new FooParams())
>
>         ->setProp1(...)
>
>         ->setProp2(...)
>
>         ->setProp3((new Obj1())
>
>         ->setProp1(...)
>
>         ->setProp2(...))
>
> );
>
>
> But now for each property I have to spend an extra 3-5 lines of code
> defining a setter, which is more code than it saved (unless the class is
> used heavily enough).
>
> I could define __construct() taking every property as a parameter, but then
> each property has to be mentioned another three times (four times with a
> doc comment), and the type twice:
>
> class FooParams {
>
>     public int $prop1;
>     // ...
>
>
>     public function __construct(
>         int $prop1
>         // ...
>     ) {
>
>         $this->prop1 = $prop1;
>         // ...
>
>     }
>
> }
>
>
> and where the object is constructed, it isn't immediately visible what the
> meaning of each positional parameter is without some IDE assistance (eg
> Ctrl+P in PhpStorm), and only specifying some parameters requires filling
> preceding ones with defaults.
>
> I could also define the __call() method to automatically expose setters,
> but then IDEs and static analysis can't understand what's going on (it
> can't see that those methods exist, what their parameters are and what they
> return). @method doc comments on the class help, but that's another line
> for every property which I have to manually keep in sync with the real
> properties.
>
> It would be great if there was a simple shorthand syntax for setting
> properties on an object in-line, without needing to extract a variable:
>
> $this->fooMethod(
>     $arg1,
>     $arg2,
>     new FooParams() {
>         prop1 = ...,
>         prop2 = ...,
>         prop3 = new Obj1() {
>             prop1 = ...,
>             prop2 = ...,
>         },
>     }
> );
>
>
> This way the structure can be written directly in the code as an expression
> and FooParams and Obj1 remain simple containers for properties.
>
> The grammar might look like (I haven't used bison/yacc before):
>
> expr_without_variable:
>         /* ... */
>     |   expr '{' inline_set_properties '}'
> ;
>
> inline_set_properties:
>         /* empty */
>     |   identifier '=' expr
>     |   identifier '=' expr ',' inline_set_properties
> ;
>
>
> (Although I think that would conflict with the alternative $var{8} syntax
> for array/string offset.)
>
> Has this been explored before? What problems can you foresee (or have been
> foreseen) with such a feature?
>
> Thanks
>

Hi Jesse,

It's fairly straightforward to implement a function that does this in
userland, for example something like the following:

function f($fqcn, $args) {
    $instance = new $fqcn;
    foreach ($args as $key => $value) {
        $instance->$key = $value;
    }
    return $instance;
}
...
$this->fooMethod(
    $arg1,
    $arg2,
    f('FooParams', [
        'prop1' => ...,
        'prop2' => ...,
        'prop3' => f('Obj1', [
            'prop1' => ...,
            'prop2' => ...,
        ],
    ])
);

You may also use the approach Peter suggested. As such, I don't think
introducing a new syntax for it is necessary.

Best,
--Matt

---

## #93680 — Ryan Pallas — 2016-06-01T15:47:30+00:00

URL: https://externals.io/message/93662#93680 · In reply to: #93679

>
>
> Hi Jesse,
>
> It's fairly straightforward to implement a function that does this in
> userland, for example something like the following:
>
> function f($fqcn, $args) {
>     $instance = new $fqcn;
>     foreach ($args as $key => $value) {
>         $instance->$key = $value;
>     }
>     return $instance;
> }
> ...
> $this->fooMethod(
>     $arg1,
>     $arg2,
>     f('FooParams', [
>         'prop1' => ...,
>         'prop2' => ...,
>         'prop3' => f('Obj1', [
>             'prop1' => ...,
>             'prop2' => ...,
>         ],
>     ])
> );
>
> You may also use the approach Peter suggested. As such, I don't think
> introducing a new syntax for it is necessary.
>

I disagree here. This lets you add dynamic properties accidentally (by
putting too many entries in the array or misspelling a property as a key),
this method does not let IDEs help you with the construction of the array,
or even what's valid for each value in that array and doesn't provide any
language level protection against misspelled properties. Of course, for the
last point, I'm assuming that this idea works like the following:

class Foo {
   public $prop1;
}

new Foo () {
    porp1 = 'foo' // undefined property error of some sort
};


> Best,
> --Matt

---

## #93683 — Fleshgrinder — 2016-06-01T17:38:37+00:00

URL: https://externals.io/message/93662#93683 · In reply to: #93680

> 
> > […]
> 
> I disagree here. This lets you add dynamic properties accidentally (by
> putting too many entries in the array or misspelling a property as a key),
> this method does not let IDEs help you with the construction of the array,
> or even what's valid for each value in that array and doesn't provide any
> language level protection against misspelled properties. Of course, for the
> last point, I'm assuming that this idea works like the following:
> 
> class Foo {
>    public $prop1;
> }
> 
> new Foo () {
>     porp1 = 'foo' // undefined property error of some sort
> };
> 
> 
> > […]
> 

Still generically solvable in userland with creativity. :)

  trait ObjectFromArray {

    public static function fromArray(array $array, ...$params) {
      $object = new static(...$params);

      foreach ($array as $k => $v) {
        if (property_exists($object, $k)) {
          $object->{$k} = $v;
        }
        else {
          trigger_error('fubar', E_USER_ERROR);
        }
      }

      return $object;
    }

  }

  class SomeObject {
    use ObjectFromArray;

    private $foo;

  }

  SomeObject::fromArray(['foo' => 'bar', 'bar' => 'foo']);

-- 
Richard "Fleshgrinder" Fussenegger

---

## #93690 — Alex Duchesne — 2016-06-01T19:02:55+00:00

URL: https://externals.io/message/93662#93690 · In reply to: #93662

> Hi internals,
>
> I often have code dealing with plain old PHP objects with properties and no
> methods, either as a substitute for keyword arguments, or to represent a
> JSON or YAML document for a web service, configuration file or schemaless
> database.
>
> At the moment, instantiating an object and setting public properties
> requires a temporary variable for each object in the structure:
>
> $obj1 = new Obj1();
> $obj1->prop1 = ...;
> $obj1->prop2 = ...;
>
> $params = new FooParams();
> $params->prop1 = ..;
> $params->prop2 = ...;
> $params->prop3 = $obj1;
>
> $this->fooMethod($arg1, $arg2, $params);
>
>
> For large structures, this gets verbose very quick. There is a good example
> of this here
> <https://github.com/jesseschalken/fail-whale/blob/72870b37c4c21d19f17324a966344ec476b432a7/src/FailWhale/Introspection.php#L22>
> involving
> 18 unnecessarily variables.
>
> I can remove the local variables by defining setters for all the properties:
>
> $this->fooMethod(
>
>      $arg1,
>      $arg2,
>      (new FooParams())
>
>          ->setProp1(...)
>
>          ->setProp2(...)
>
>          ->setProp3((new Obj1())
>
>          ->setProp1(...)
>
>          ->setProp2(...))
>
> );
>
>
> But now for each property I have to spend an extra 3-5 lines of code
> defining a setter, which is more code than it saved (unless the class is
> used heavily enough).
>
> I could define __construct() taking every property as a parameter, but then
> each property has to be mentioned another three times (four times with a
> doc comment), and the type twice:
>
> class FooParams {
>
>      public int $prop1;
>      // ...
>
>
>      public function __construct(
>          int $prop1
>          // ...
>      ) {
>
>          $this->prop1 = $prop1;
>          // ...
>
>      }
>
> }
>
>
> and where the object is constructed, it isn't immediately visible what the
> meaning of each positional parameter is without some IDE assistance (eg
> Ctrl+P in PhpStorm), and only specifying some parameters requires filling
> preceding ones with defaults.
>
> I could also define the __call() method to automatically expose setters,
> but then IDEs and static analysis can't understand what's going on (it
> can't see that those methods exist, what their parameters are and what they
> return). @method doc comments on the class help, but that's another line
> for every property which I have to manually keep in sync with the real
> properties.
>
> It would be great if there was a simple shorthand syntax for setting
> properties on an object in-line, without needing to extract a variable:
>
> $this->fooMethod(
>      $arg1,
>      $arg2,
>      new FooParams() {
>          prop1 = ...,
>          prop2 = ...,
>          prop3 = new Obj1() {
>              prop1 = ...,
>              prop2 = ...,
>          },
>      }
> );



Is there anything wrong with casting an array to an object? The syntax 
is almost identical to what you want to achieve.

I can think of three ways to achieve, with the current PHP stable, what 
you seem to desire (forgive me if I misunderstood). Your code becomes:

    $this->fooMethod(
         $arg1,
         $arg2,
         (object)[
             'prop1' => ...,
             'prop2' => ...,
             'prop3' => (object)[
                 'prop1' => ...,
                 'prop2' => ...,
             },
         }
    );


Pretty concise and close to your proposal, right?
Another option is anonymous classes

    $this->fooMethod(
         $arg1,
         $arg2,
         new class {
             var $prop1 => ...,
             var $prop2' => ...,
             var $prop3' => new class {
            	  var $prop1' => ...,
           	  var $prop2' => ...,
             },
         }
    );


Or if you need a specific class name for validation or anything:

$this->fooMethod(
     $arg1,
     $arg2,
     new class extends FooClass {
         public $prop1 => ...,
                $prop2' => ...,
                $prop3' => new class extends FooBar {
        	                          public $prop1' => ...,
       	                                 $prop2' => ...;
                           },
     }
);



Last option is a generic class with a __construct taking an array and 
creating a populated instance, which can be used as a base/trait to 
extend dumb classes that only require a name. But you've covered that 
already.


Cheers,


> This way the structure can be written directly in the code as an expression
> and FooParams and Obj1 remain simple containers for properties.
>
> The grammar might look like (I haven't used bison/yacc before):
>
> expr_without_variable:
>          /* ... */
>      |   expr '{' inline_set_properties '}'
> ;
>
> inline_set_properties:
>          /* empty */
>      |   identifier '=' expr
>      |   identifier '=' expr ',' inline_set_properties
> ;
>
>
> (Although I think that would conflict with the alternative $var{8} syntax
> for array/string offset.)
>
> Has this been explored before? What problems can you foresee (or have been
> foreseen) with such a feature?
>
> Thanks

---

## #93693 — Jesse Schalken — 2016-06-01T20:09:44+00:00

URL: https://externals.io/message/93662#93693 · In reply to: #93666

> Morphed into simply passing an array to the constructor ...
>
>         $static1         = new Data\StaticVariable( array(
>           'name'         => 'variable name',
>           'value'        => $unknown1,
>           'functionName' => 'blahFunction' ) );
>
>         // className defaults to null
>
> Of cause the short cut at that time was to drop the names and simply
> count the variables, but what this replaces is all the complications of
> named parameters and allowed passing objects in such as '$unknown1'
>

This works fine to remove the temporary variable but compromises two things
that I think are very important:

   1. IDE tooling and static analysis. An IDE can't autocomplete property
   names as array keys, because it doesn't know the array keys are properties,
   nor can it ensure that the properties actually exist, are visible, and are
   being assigned the correct type. If I do a "Find Usages" on a property, it
   may appear to be only read and never set, implying I can remove the
   property and replace all the reads with the default value. Any static
   analysis tool would have the same trouble.
   2. Performance. The code spends time building a temporary hash table
   just to loop over it, look up each key in the object's property table,
   check visibility, check type, assign it, and discard the hash table. That's
   a lot of overhead just to assign some properties. With a dedicated inline
   property assignment syntax, a normal bunch of "assign property" bytecode
   instructions can be output and the PHP runtime will cache the property
   offsets and assign them directly, and can elide the property type check if
   the optimiser knows it will pass. HHVM can even burn the property offset
   right into the compiled machine code. That's *way *faster. (At least
   that's how I was told it works when I asked here a couple of weeks ago.)



> --
> Lester Caine - G8HFL
> -----------------------------
> Contact - http://lsces.co.uk/wiki/?page=contact
> L.S.Caine Electronic Services - http://lsces.co.uk
> EnquirySolve - http://enquirysolve.com/
> Model Engineers Digital Workshop - http://medw.co.uk
> Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

---

## #93694 — Jesse Schalken — 2016-06-01T20:20:40+00:00

URL: https://externals.io/message/93662#93694 · In reply to: #93667

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.

---

## #93695 — Jesse Schalken — 2016-06-01T20:36:39+00:00

URL: https://externals.io/message/93662#93695 · In reply to: #93678

On Thu, Jun 2, 2016 at 12:38 AM, guilhermeblanco@gmail.com <
guilhermeblanco@gmail.com> wrote:

> Hi,
>
> I very much liked the proposal, specially if we do consider a potential
> reusability if anyone ever decide to revisit Attributes RFC. It's out of
> scope how I'd see this implemented, so let me focus on the proposal only.
> There're some edge cases that need to be discussed and considered here.
> Here's one quick example:
>
> declare("foo", "bar");
>
> class Foo {
>     public $bar;
> }
>
> $f = new Foo() {
>     foo = "bar"
> };
>
> Should "foo" be considered as a property identifier or as a constant,
> which would then be converted into "bar" and then used as $f->bar = "bar"?
>

My intention was for "expr->prop = ..." to be equivalent to "expr { prop =
... }", except obviously the result is the object itself and multiple
properties can be assigned. So the property name is written directly as an
identifier, and wouldn't be looked up as a constant. If expression property
names are required I suppose it should mirror the "expr->{expr} = ...;"
syntax and become "expr { {expr} ... }".

Turns out there is already a "property_name" production
in zend_language_parser.y (and also "member_name", but I'm not sure what
the difference is), so the grammar would become:

expr_without_variable:
        /* ... */
    |   expr '{' inline_set_properties '}'
;

inline_set_properties:
        /* empty */
    |   property_name '=' expr
    |   property_name '=' expr ',' inline_set_properties
;




>
> Regards,
>
>
> On Wed, Jun 1, 2016 at 5:49 AM, Peter Cowburn <petercowburn@gmail.com>
> wrote:
>
> > […]
>
>
>
> --
> Guilherme Blanco
> Lead Architect at E-Block

---

## #93696 — Lester Caine — 2016-06-01T20:41:34+00:00

URL: https://externals.io/message/93662#93696 · In reply to: #93693

> 
> 
> > […]
> 
> This works fine to remove the temporary variable but compromises two things
> that I think are very important:

Which was the start of the discussion when 'arrays' were being pushed as
a 'solution' back in the PHP5.1/2 timescale.

>    1. IDE tooling and static analysis. An IDE can't autocomplete property
>    names as array keys, because it doesn't know the array keys are properties,
>    nor can it ensure that the properties actually exist, are visible, and are
>    being assigned the correct type. If I do a "Find Usages" on a property, it
>    may appear to be only read and never set, implying I can remove the
>    property and replace all the reads with the default value. Any static
>    analysis tool would have the same trouble.

Hard coding the list of names just so you can add 'static analysis' is
only one style of working. My set of array keys come from the database
schema, which is another way of defining and typing your parameter set.

>    2. Performance. The code spends time building a temporary hash table
>    just to loop over it, look up each key in the object's property table,
>    check visibility, check type, assign it, and discard the hash table. That's
>    a lot of overhead just to assign some properties. With a dedicated inline
>    property assignment syntax, a normal bunch of "assign property" bytecode
>    instructions can be output and the PHP runtime will cache the property
>    offsets and assign them directly, and can elide the property type check if
>    the optimiser knows it will pass. HHVM can even burn the property offset
>    right into the compiled machine code. That's *way *faster. (At least
>    that's how I was told it works when I asked here a couple of weeks ago.)

If your starting point is an array of database fields which are enhanced
with the relevant type and constraint attributes, then 'performance'
starts from that base. That was the base we were working on 15 years
ago, and trying to replace that with hard coded 'PHP only' code does not
remove that. The drive to make everything as 'static' as possible misses
the whole point that PHP is a DYNAMIC scripting system.

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

---

## #93698 — Rasmus Schultz — 2016-06-01T21:09:28+00:00

URL: https://externals.io/message/93662#93698 · In reply to: #93696

> The drive to make everything as 'static' as possible misses
> the whole point that PHP is a DYNAMIC scripting system.

I think that line of argumentation is pretty outdated - it may have been
valid many years ago, but by now, PHP is not purely dynamic; far from it.
It's a gradually-typed language, and more so with scalar type-hints
recently added, and typed properties and nullable types coming up.

The drive to enhance the static side of the language doesn't miss a point,
nor does it take away from the dynamic side of the language - the whole
point of gradually-typed languages is the ability to choose between static
and dynamic in each situation.

This tendency to "defend" the dynamic side of the language, as though
static features were somehow an "attack" on dynamic features, befuddles me.

These two sides of the language do not compete with each other - they
complement each other.

Just interjecting a general remark here, I'm not commenting on the proposed
feature specifically.




> 
> > […]
> things
> > […]
>
> Which was the start of the discussion when 'arrays' were being pushed as
> a 'solution' back in the PHP5.1/2 timescale.
>
> > […]
> properties,
> > […]
> and are
> > […]
> property, it
> > […]
>
> Hard coding the list of names just so you can add 'static analysis' is
> only one style of working. My set of array keys come from the database
> schema, which is another way of defining and typing your parameter set.
>
> > […]
> That's
> > […]
> inline
> > […]
> bytecode
> > […]
> check if
> > […]
> offset
> > […]
> ago.)
>
> If your starting point is an array of database fields which are enhanced
> with the relevant type and constraint attributes, then 'performance'
> starts from that base. That was the base we were working on 15 years
> ago, and trying to replace that with hard coded 'PHP only' code does not
> remove that. The drive to make everything as 'static' as possible misses
> the whole point that PHP is a DYNAMIC scripting system.
>
> --
> Lester Caine - G8HFL
> -----------------------------
> Contact - http://lsces.co.uk/wiki/?page=contact
> L.S.Caine Electronic Services - http://lsces.co.uk
> EnquirySolve - http://enquirysolve.com/
> Model Engineers Digital Workshop - http://medw.co.uk
> Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

---

## #93700 — Lester Caine — 2016-06-01T21:28:14+00:00

URL: https://externals.io/message/93662#93700 · In reply to: #93698

> This tendency to "defend" the dynamic side of the language, as though
> static features were somehow an "attack" on dynamic features, befuddles me.
> 
> These two sides of the language do not compete with each other - they
> complement each other.

That is perhaps what *I* am looking for. Being able to manage the
attributes of a variable be it statically or dynamically defined should
be identical. My 'attack' is that all the current discussions are on
developing the static side without any mention of adding the same
constraint system to a list of dynamic properties.

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
