Bug #79867 [Opn->Nab]: Incorrect constructor promotion

From: Date: Thu, 16 Jul 2020 20:36:08 +0000
Subject: Bug #79867 [Opn->Nab]: Incorrect constructor promotion
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-228082@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79867&edit=1 ID: 79867 Updated by: nikic@php.net Reported by: slavcopost at gmail dot com Summary: Incorrect constructor promotion -Status: Open +Status: Not a bug Type: Bug Package: *General Issues Operating System: * PHP Version: 8.0.0alpha2 Block user comment: N Private report: N New Comment: 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 } } Previous Comments: ------------------------------------------------------------------------ [2020-07-16 20:31:46] 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" ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79867&edit=1

« previous php.bugs (#228082) next »