|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79169 When we replace property visibility in child class we still get [null]
Submitted: 2020-01-25 20:10 UTC Modified: 2020-01-25 21:31 UTC
From: 6562680 at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.3.14 OS: Win10
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: 6562680 at gmail dot com
New email:
PHP Version: OS:


 [2020-01-25 20:10 UTC] 6562680 at gmail dot com
Usually problem happened with vendor libraries where authors try to write the code with private variables.

Next day we want to rewrite the class with own constructor and own injected services.

Ok, we can change visibility of all private properties.
Then we can put into __construct own dependencies, set it to these properties.

1) We still need to call parent::__construct() otherwise we risk to lost half class functionality

2) We still need access to services that could be injected into library somewhere in /vendor/ folder. We usually "change" visibility level and expected to see these dependencies in our code. But nope. We should fill these props manually even if the properties was filled with dependency injector inside the library

Test script:

class P
    private $a;
    protected $b;
    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;

class C extends P
    protected $a;
    protected $b;
    private $c;
    public function __construct($c)
        $this->c = $c;
        parent::__construct(1, 2);
        var_dump($this->a); // null, wtf, we pass 1 to parent!

var_dump(new C(1));

Expected result:
Changing visibility is actually CHANGING visibility.

Not property behavior replacement.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-25 21:31 UTC]
-Status: Open +Status: Not a bug -Type: Feature/Change Request +Type: Bug
 [2020-01-25 21:31 UTC]
private $a is private to class P. Class C cannot see or use it, and so it cannot "change visibility" of it.
This is an important feature of object-oriented programming.
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Wed Sep 28 21:05:52 2022 UTC