php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79867 Promoted untyped properties should get null default value
Submitted: 2020-07-16 20:31 UTC Modified: 2020-07-17 13:04 UTC
From: slavcopost at gmail dot com Assigned:
Status: Closed Package: Class/Object related
PHP Version: 8.0.0alpha2 OS: *
Private report: No CVE-ID: None
 [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"


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-07-16 20:36 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2020-07-16 20:36 UTC] 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:36 UTC] requinix@php.net
-Package: *General Issues +Package: Class/Object related
 [2020-07-16 20:36 UTC] requinix@php.net
You still have to call the parent constructor.
 [2020-07-16 20:39 UTC] slavcopost at gmail dot com
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.
 [2020-07-16 20:41 UTC] slavcopost at gmail dot com
> You are missing a parent::__construct() call.

That has never been mandatory, has it?
 [2020-07-16 20:53 UTC] nikic@php.net
> That has never been mandatory, has it?

No, and it's still not mandatory -- for example, you could also do a manual $this->a = 42 assignment if you like. The state of the parent class just needs to be initialized *somehow* and parent::__construct() is the most typical and most robust way to do so.
 [2020-07-16 20:57 UTC] nikic@php.net
-Status: Not a bug +Status: Re-Opened
 [2020-07-16 20:57 UTC] nikic@php.net
I can see the argument for giving $a a null default value though, because that's what normally happens with untyped properties. Right now constructor promotion gives you a property declaration that (for untyped properties) you cannot achieve in any other way, which is certainly odd, and not really intentional.
 [2020-07-16 21:00 UTC] slavcopost at gmail dot com
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.
 [2020-07-16 21:03 UTC] slavcopost at gmail dot com
Yep, Nikita. I missed you last comment, it's exactly what I mean.
 [2020-07-17 13:04 UTC] nikic@php.net
-Summary: Incorrect constructor promotion +Summary: Promoted untyped properties should get null default value
 [2020-07-17 13:10 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=86a62eb1fcf923d7b54d63df2277d73913879d7b
Log: Fixed bug #79867
 [2020-07-17 13:10 UTC] nikic@php.net
-Status: Re-Opened +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 08:01:29 2024 UTC