Req->Bug #79169 [Opn->Nab]: When we replace property visibility in child class we still get [null]

From: Date: Sat, 25 Jan 2020 21:31:08 +0000
Subject: Req->Bug #79169 [Opn->Nab]: When we replace property visibility in child class we still get [null]
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-225112@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79169&edit=1 ID: 79169 Updated by: requinix@php.net Reported by: 6562680 at gmail dot com Summary: When we replace property visibility in child class we still get [null] -Status: Open +Status: Not a bug -Type: Feature/Change Request +Type: Bug Package: *General Issues Operating System: Win10 PHP Version: 7.3.14 Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2020-01-25 20:10:08] 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. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79169&edit=1

« previous php.bugs (#225112) next »