Bug #80423 [NEW]: Regarding Access modifier

From: Date: Thu, 26 Nov 2020 12:15:44 +0000
Subject: Bug #80423 [NEW]: Regarding Access modifier
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-230637@lists.php.net to get a copy of this message
From: wajidsayyed980 at gmail dot com Operating system: Windows 10 Pro PHP version: 7.2.34 Package: *General Issues Bug Type: Bug Bug description:Regarding Access modifier 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 bug report at https://bugs.php.net/bug.php?id=80423&edit=1 -- Fix committed: https://bugs.php.net/fix.php?id=80423&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=80423&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=80423&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=80423&r=needscript Try newer version: https://bugs.php.net/fix.php?id=80423&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=80423&r=support Expected behavior: https://bugs.php.net/fix.php?id=80423&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=80423&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=80423&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=80423&r=globals PHP version support discontinued: https://bugs.php.net/fix.php?id=80423&r=phptooold Daylight Savings: https://bugs.php.net/fix.php?id=80423&r=dst IIS Stability: https://bugs.php.net/fix.php?id=80423&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=80423&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=80423&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=80423&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=80423&r=mysqlcfg

« previous php.bugs (#230637) next »