#49702 [Bgs]: I have access to the private attributes of a give class
| From: | vito_web at yahoo dot com | Date: | Tue, 29 Sep 2009 07:04:33 +0000 |
| Subject: | #49702 [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-143024@lists.php.net to get a copy of this message | ||
ID: 49702
User updated by: vito_web at yahoo dot com
Reported By: vito_web at yahoo dot com
Status: Bogus
Bug Type: Class/Object related
Operating System: Linux
PHP Version: 5.3.0
New Comment:
Sorry, I thought that it works like in java. In java such code would
not
even compile. Not very good decision for OOP in PHP to create another
private property at the time when the parent has already such property
with the same name. Anyway thanks
Previous Comments:
------------------------------------------------------------------------
[2009-09-28 19:02:06] sjoerd@php.net
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.
------------------------------------------------------------------------
[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