Re: [RFC] Constructor Property Promotion

From: Date: Thu, 26 Mar 2020 18:01:47 +0000
Subject: Re: [RFC] Constructor Property Promotion
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-109348@lists.php.net to get a copy of this message
On Thu, Mar 26, 2020 at 5:02 PM David Rodrigues <david.proweb@gmail.com> wrote: > Hello! > > With all respect, seems a bit confuses to me, without in fact offer a new > feature, just a shortcut. I hope it's me seeing it the wrong way. > > What happen if I rename the constructor parameters on a child class > constructor? > > Example (https://3v4l.org/VqQde): > > class A { > public function __construct(public int $x) { ... } > } > > class B extends A { > public function __construct(public int $y) { ...; > parent::__construct($y); } > } > Constructors (in general, independent of this feature) are per-class and do not participate in classical signature-based inheritance (the exception being abstract constructors, but those aren't relevant here). The notion of "renaming" a constructor parameter doesn't make sense in that regard, because the parameters of the child constructor have absolutely no relation to the parameters of the parent constructor. The way to write your example would be: class A { public function __construct(public int $x) {} } class B extends A { public function __construct(int $x) { parent::__construct($x); // Your extra logic here. } } Regards, Nikita

« previous php.internals (#109348) next »