Bug #79867 [Com]: Incorrect constructor promotion
| From: | slavcopost at gmail dot com | Date: | Thu, 16 Jul 2020 20:39:26 +0000 |
| Subject: | Bug #79867 [Com]: Incorrect constructor promotion | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-228084@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
Comment by: slavcopost at gmail dot com
Reported by: slavcopost at gmail dot com
Summary: Incorrect constructor promotion
Status: Not a bug
Type: Bug
Package: Class/Object related
Operating System: *
PHP Version: 8.0.0alpha2
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2020-07-16 20:36:40] requinix@php.net
You still have to call the parent constructor.
------------------------------------------------------------------------
[2020-07-16 20:36:08] nikic@php.net
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
}
}
------------------------------------------------------------------------
[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