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] [email protected]

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

Reply via email to