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
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.
Password:
Status:
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-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 12:01:27 2024 UTC