4

I created the follow class PHP ver 5.5

abstract class Model
{
    var $id;

    private $cName;
    private $tName;

    public function __construct($id = 0)
    {
        $this->cName = 'Im cName';
        $this->tName = 'Im tName';            
    }
}

then an extended class

class claseExtend extends Model
{
    var $id;

    public function hola()
    {
        $this->id = 1;
        return (array) $this;
    }
}

if I Execute this:

$obj = new claseExtend() ;
$retHola =$obj->hola();
print_r($retHola);

I was Expecting to get: array(id => 1)

But the output is: array( \u0000Model\u0000cName: => 'Im cName', \u0000Model\u0000tName => 'Im tName')

What Am I doing wrong, or why is this happening if the attributes are private?

¿Why the array cast includes the private properties?

thanks for your help.

Oscar Gallardo
  • 2,240
  • 3
  • 27
  • 47
  • Try to serialize / dump the object from the outside, inside `hola` you are in the objects scope and have access the the protected/private methods and properties of that object. Ie: `$foo = new claseExtend(); print_r((array) $foo);` – JimL Jun 03 '16 at 20:45
  • 1
    Related: http://stackoverflow.com/q/10249647/697370 – Jeff Lambert Jun 03 '16 at 20:51
  • 1
    `private` variables are still inherited and available within the class. What you can't do is access them from outside the class like this: `$foo = new claseExtend; echo $foo->cName;` – miken32 Jun 03 '16 at 21:00
  • 1
    Actually ,private properties are not inherited, but protected properties are. you can see that if you run print($this->cName) you will get undefined variable, so I think print_r is just showing parent class private properties for debugging purposes – ReZa Jun 03 '16 at 21:06
  • They are inherited, but no accessible from child classes. – Ryan Jun 03 '16 at 21:11
  • thanks for your comments I Have edited the question to manage the return of the function. I think the cast (array) is making something strange there – Oscar Gallardo Jun 03 '16 at 21:19
  • Your code should be throwing errors about trying to access `$this` in a static function (hola)? Please switch on error reporting. What version of PHP are you using? – Ryan Vincent Jun 03 '16 at 21:36
  • imo, It would help us all if we start with valid PHP code before trying to explain the behaviour? – Ryan Vincent Jun 03 '16 at 21:43
  • imo, Explaining why PHP does the actions it does, when all the code is valid, can be 'interesting'. ... This situation ... I have no idea. I suspect most of it is not important. i.e. just a consequence of how PHP works. Whatever, the code isn't valid. – Ryan Vincent Jun 03 '16 at 21:50
  • I'm sorry, I have fixed the bug. I'm using 5.5 php version – Oscar Gallardo Jun 04 '16 at 14:12

2 Answers2

5

print_r is a special magic function that will show the class including private and protected properties. It's a debugging utility function. Quoting from the manual:

print_r(), var_dump() and var_export() will also show protected and private properties of objects with PHP 5. Static class members will not be shown.

In PHP, private properties are inherited in derived classes, but they are not accessible.

Ryan
  • 14,392
  • 8
  • 62
  • 102
  • It is curious that if I change the print_r for a return is return the same thing – Oscar Gallardo Jun 03 '16 at 21:13
  • But he's not using `print_r()` on the object. He's converting the object to an array with `(array)$this`, and printing that. The question is why the cast includes the private properties. – Barmar Jun 03 '16 at 21:25
4

When you cast an object to an array, private and protected properties are included in the result. The documentation says:

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.

Those null bytes are shown in your results as \u0000.

Barmar
  • 741,623
  • 53
  • 500
  • 612