|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2020-07-16 20:31 UTC] slavcopost at gmail dot com
Description: ------------ Confusing with constructor promostion result. I got warning where I shouldn't, as far I undestand the RFC. snippet: https://3v4l.org/SKTie Test script: --------------- <?php class A { public function __construct( public $a = 1 ) {} } class B extends A { public function __construct( public string $b = 'hello' ) {} } $b = new B(); var_dump($b->a, $b->b); Expected result: ---------------- As far I understand it's the same code after deshugaring w/o any warnings with output ``` NULL string(5) "hello" ``` snippet: https://3v4l.org/HM5s0 ```php <?php class A { public function __construct( public $a = 1 ) {} } class B extends A { public function __construct( public string $b = 'hello' ) {} } $b = new B(); var_dump($b->a, $b->b); ``` Actual result: -------------- Warning: Undefined property: B::$a in /in/SKTie on line 16 NULL string(5) "hello" PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 18:00:02 2025 UTC |
You are missing a parent::__construct() call. Possibly class B extends A { public function __construct( public string $b = 'hello' ) { parent::__construct(); // Use default for A::$a } } or class B extends A { public function __construct( $a, public string $b = 'hello' ) { parent::__construct($a); // Set A::$a = $a } }Sorry I meant the deshugaring code is like ```php <?php class A { public $a; public function __construct( $a = 1 ) { $this->a = $a; } } class B extends A { public function __construct( public string $b = 'hello' ) {} } $b = new B(); var_dump($b->a, $b->b); ``` And this code does not show warning.Alright, but w/o constructor promotion, when I define property it's auth initilized. I.e. ``` class A { public $a; } ``` does not require an additional initialization.