Bug #80423 [Opn->Nab]: Regarding Access modifier

From: Date: Thu, 26 Nov 2020 12:33:48 +0000
Subject: Bug #80423 [Opn->Nab]: Regarding Access modifier
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-230639@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80423&edit=1 ID: 80423 Updated by: cmb@php.net Reported by: wajidsayyed980 at gmail dot com Summary: Regarding Access modifier -Status: Open +Status: Not a bug Type: Bug Package: *General Issues Operating System: Windows 10 Pro PHP Version: 7.2.34 -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: Well, that behaves as designed. If the property on Vehicle is private, the assignment in the global scope actually creates a public property in Car: <https://3v4l.org/OieYm>. Previous Comments: ------------------------------------------------------------------------ [2020-11-26 12:15:44] wajidsayyed980 at gmail dot com Description: ------------ While I am using the Public, Private, and Protected access modifier I found an error which is : Error is between Private and Protected modifiers By default, the scope of the Private variable should be within the class and not accessible to derived or child class. And The scope of the Protected variable should be within the class and its derived class. BUT It's acting vice-versa The private variable is accessible through the derived class which is wrong and the protected variable is not accessible through the derived class. Please check and revert back with a solution. PFB the code snippet for reference: // parent class class Vehicle { // protected property name protected $name; // public function start public function start() { echo $this->name. " - Engine start...<br/>"; } // public function stop public function stop() { echo $this->name. " - Engine stop...<br/>"; } } // child class class Car extends Vehicle { public function drive() { // accessing name variable of Car class echo "I am " . $this->name . "<br/>"; echo "Lets go on a drive..."; } } $car = new Car(); $car->name = "Mercedes benz"; // calling parent class method $car->start(); // calling child class method $car->drive(); Test script: --------------- // parent class class Vehicle { // private property name private $name; // public function start public function start() { echo $this->name. " - Engine start...<br/>"; } // public function stop public function stop() { echo $this->name. " - Engine stop...<br/>"; } } // child class class Car extends Vehicle { public function drive() { // accessing name variable of Car class echo "I am " . $this->name . "<br/>"; echo "Lets go on a drive..."; } } $car = new Car(); $car->name = "Mercedes benz"; // calling parent class method $car->start(); // calling child class method $car->drive(); Expected result: ---------------- If I used the protected keyword it should result in the following output: - Engine start... I am Mercedes benz Lets go on a drive... Actual result: -------------- While using the protected keyword it throws below error: Fatal error: Uncaught Error: Cannot access protected property Car::$name in C:\xampp\htdocs\PHP Programming\PHP OOPS\problem4.php:37 Stack trace: #0 {main} thrown in C:\xampp\htdocs\PHP Programming\PHP OOPS\problem4.php on line 37 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=80423&edit=1

« previous php.bugs (#230639) next »