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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rolandszabo62 at gmail dot com
New email:
PHP Version: OS:

 

 [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: Fri Mar 29 04:01:29 2024 UTC