|  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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: rolandszabo62 at gmail dot com
New email:
PHP Version: OS:


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

Test script:
    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 () {

    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();

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

Actual result:
it works for some reason.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-11-11 03:00 UTC]
-Status: Open +Status: Not a bug
 [2021-11-11 03:00 UTC]
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-2023 The PHP Group
All rights reserved.
Last updated: Sat Jun 03 07:03:39 2023 UTC