Bug #81608 [Opn->Nab]: I can access protected variable as public.
| From: | requinix@php.net | Date: | Thu, 11 Nov 2021 03:00:23 +0000 |
| Subject: | Bug #81608 [Opn->Nab]: I can access protected variable as public. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-237675@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=81608&edit=1
ID: 81608
Updated by: requinix@php.net
Reported by: rolandszabo62 at gmail dot com
Summary: I can access protected variable as public.
-Status: Open
+Status: Not a bug
Type: Bug
Package: Class/Object related
Operating System: CentOS Linux release 8.4.2105
PHP Version: 7.4.25
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2021-11-11 02:17:53] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81608&edit=1