Hi Stas,


>> https://wiki.php.net/rfc/constructor-promotion
>
>1. From the first glance, it doesn't seem clear how this syntax would
>interact with magic methods - i.e., if you have __get, would access to
>$make call it? If not, it's rather un-intuitive since the property is
>not defined in the class but magic method is not called.


In that case, __get() would not be called, since the property is defined
on the class, just not in a traditional way.  This behavior is not
unprecedented however, as implicitly created public properties follow the
same pattern:

class Foo {
  public function __get($key) {
    echo "Asked for $key\n";
    return null;
  }
  public function __construct() {
    $this->bar = 123;
  }
}

$f = new foo;
var_dump($f); // outputs int(123), doesn't hit __get()


>
>2. What would happen with this code:
>
>class Base { public __construct(public $f) {} }
>class PublicBase extends Base {
>  public __construct($param) { __parent::__construct($param); }
>}
>
>class Child extends PublicBase { public __construct(public $f) {
>$this->f = 42; parent::__construct($f); } }
>
>Note here that whoever writes class Child may not be aware that class
>Base even exists and how it is implemented, since he implements against
>PublicBase.

The object would have a public property $f with the value of whatever was
passed into the constructor. This is the same result as if the Base class
had been declared as:

class Base {
public $f;
public function __construct($f) {
$this->f = $f;
}
}

So the observed behavior from the Child class would have the same result
either way. They invoked the parent::__construct() after setting $this->f,
and that constructor overrode their property set.


>3. What happens if I need to have some arguments that are not properties?

You do that. The following syntax is pefectly valid:

class Foo {
public function __construct(public $f, $g) {
// $this->f implicitly set to $f
echo "The second arg to the constructor was $g";
}
}

Or just a normal contructor with no promotion:

class Foo {
public function __construct($p) {
echo "The arg to the constructor was $p";
}
}


>4. How would one extract phpdoc descriptions for such properties?

You wouldn't have individual Doc Comments per property, but an editor
could parse __construct's Doc Comment. This is arguably more work for the
IDE and/or PhpDoc generator, but the data is still there.


Thanks,
Sean Cannella | Facebook Eng
If you are smart and persistent, you may find the truth. It is not always
a reward.


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to