Bug #81608 [NEW]: I can access protected variable as public.

From: Date: Thu, 11 Nov 2021 02:17:54 +0000
Subject: Bug #81608 [NEW]: I can access protected variable as public.
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-237674@lists.php.net to get a copy of this message
From:             rolandszabo62 at gmail dot com
Operating system: CentOS Linux release 8.4.2105
PHP version:      7.4.25
Package:          Class/Object related
Bug Type:         Bug
Bug description:I can access protected variable as public.

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.

-- 
Edit bug report at https://bugs.php.net/bug.php?id=81608&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=81608&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=81608&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=81608&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=81608&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=81608&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=81608&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=81608&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=81608&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=81608&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=81608&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=81608&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=81608&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=81608&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=81608&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=81608&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=81608&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=81608&r=mysqlcfg


Thread (2 messages)

« previous php.bugs (#237674) next »