php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80423 Regarding Access modifier
Submitted: 2020-11-26 12:15 UTC Modified: 2020-11-26 12:33 UTC
From: wajidsayyed980 at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: *General Issues
PHP Version: 7.2.34 OS: Windows 10 Pro
Private report: No CVE-ID: None
 [2020-11-26 12:15 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-11-26 12:33 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-11-26 12:33 UTC] cmb@php.net
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>.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Jul 05 10:05:45 2022 UTC