php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81608 I can access protected variable as public.
Submitted: 2021-11-11 02:17 UTC Modified: 2021-11-11 03:00 UTC
From: rolandszabo62 at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 7.4.25 OS: CentOS Linux release 8.4.2105
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
3 + 22 = ?
Subscribe to this entry?

 
 [2021-11-11 02:17 UTC] rolandszabo62 at gmail dot com
Description:
------------
$dog->database->insert(); ////WHY THIS WORKS? "database" beeing a protected variable in Model class

Test script:
---------------
<?php
    final class Database {
        public $connection = "database connection stream";
        public function insert () {
            echo "inserted\n";
        }
    }

    abstract class Model {
        protected $database = null;
        public function __construct () {
            $this->database = new Database();
        }
    }
    
    class Dog extends Model {
        public function __construct () {
            parent::__construct();
        }
    }

    class Cat extends Model {
        public function testProtected() {
            $dog = new Dog();
            $dog->database->insert(); ////WHY THIS WORKS? "database" beeing a protected variable in Model class
        }
    }
    
    $cat = new Cat();
    $cat->testProtected();
?>

Expected result:
----------------
I expect that $dog->database->insert(); gives me "Cannot access protected property Dog::$database

Actual result:
--------------
it works for some reason.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-11-11 03:00 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2021-11-11 03:00 UTC] requinix@php.net
protected means that child classes can access it. $database is defined in Model and Cat is a child class of Model. Doesn't matter that $dog is an instance of Dog.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 23:01:34 2024 UTC