php.net |  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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: 6562680 at gmail dot com
New email:
PHP Version: OS:

 

 [2020-01-25 20:10 UTC] 6562680 at gmail dot com
Description:
------------
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:
---------------
<?php

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($this->b);
    }
}

var_dump(new C(1));


Expected result:
----------------
Changing visibility is actually CHANGING visibility.

Not property behavior replacement.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-25 21:31 UTC] requinix@php.net
-Status: Open +Status: Not a bug -Type: Feature/Change Request +Type: Bug
 [2020-01-25 21:31 UTC] requinix@php.net
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-2021 The PHP Group
All rights reserved.
Last updated: Tue Apr 20 11:01:23 2021 UTC