#49702 [Opn->Bgs]: I have access to the private attributes of a give class

From: Date: Mon, 28 Sep 2009 19:02:06 +0000
Subject: #49702 [Opn->Bgs]: I have access to the private attributes of a give class
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-143008@lists.php.net to get a copy of this message
ID: 49702 Updated by: sjoerd@php.net Reported By: vito_web at yahoo dot com -Status: Open +Status: Bogus Bug Type: Class/Object related Operating System: Linux PHP Version: 5.3.0 New Comment: Thank you for your report. The behavior you describe is not a bug. The statement $obj2->private = "Hacked"; does not change the private property of MyClass. Instead, it creates a new public property on MyClass2. Previous Comments: ------------------------------------------------------------------------ [2009-09-28 18:25:04] vito_web at yahoo dot com <?php class MyClass { private $private = 'Private'; } class MyClass2 extends MyClass { protected $protected = 'Protected2'; } $obj2 = new MyClass2(); $obj2->private = "Hacked"; echo $obj2->private; // should be fatal error by accessing the private property, but it displays the string "Hacked" ?> ------------------------------------------------------------------------ [2009-09-28 18:11:35] vito_web at yahoo dot com Description: ------------ <?php /** * Define MyClass */ class MyClass { public $public = 'Public'; protected $protected = 'Protected'; private $private = 'Private'; function printHello() { echo $this->public; echo $this->protected; echo $this->private; } } $obj = new MyClass(); echo $obj->public; // Works //echo $obj->protected; // Fatal Error //echo $obj->private; // Fatal Error $obj->printHello(); // Shows Public, Protected and Private /** * Define MyClass2 */ class MyClass2 extends MyClass { // We can redeclare the public and protected method, but not private protected $protected = 'Protected2'; function printHello() { echo $this->public; echo $this->protected; echo $this->private; } } $obj2 = new MyClass2(); echo $obj2->public; // Works echo $obj2->private = "Hacked"; echo $obj2->private; // Undefined //echo $obj2->protected; // Fatal Error $obj2->printHello(); // Shows Public, Protected2, Undefined ?> Expected result: ---------------- PHP Fatal error: Cannot access private property MyClass2::$private in...... Actual result: -------------- PublicPublicProtectedPrivatePublicHackedHackedPublicProtected2Hacked ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=49702&edit=1

« previous php.bugs (#143008) next »