Private attributes of parent class unavailable when inheriting

Discussion in 'PHP' started by elum.chaitu, Sep 6, 2010.

  1. #1
    Hi,
    when the parent class has some private attributes, we can't access them using XDebug. However var_dump works fine in this case.

    the code I've used is:
    class abc {
    private $arr;
    public function abc(){
    $this->arr = array(
    0 => array("some", "values"),
    1 => array("some", "more", "values")
    );
    }
    }

    class def extends abc {

    }

    $o = new def;
    echo "o: ";
    var_dump($o);




    Have a look at the end of the log:
    there is no "fullname" property in that case...

    [ log ] property_get -i 10 -n $o
    [ log ] <?xml version="1.0" encoding="iso-8859-1"?>
    <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" [^] command="property_get" transaction_id="10"><property name="$o" fullname="$o" address="148281248" type="object" children="1" classname="def" numchildren="1"><property name="CLASSNAME" type="string"><![CDATA[def]]></property><property name="arr" facet="private" address="148282540"></property></property></response>



    Comparing to what happens when I use "abc" (base) class:

    [ log ] property_get -i 10 -n $o
    [ log ] <?xml version="1.0" encoding="iso-8859-1"?>
    <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" [^] command="property_get" transaction_id="10"><property name="$o" fullname="$o" address="148281248" type="object" children="1" classname="abc" numchildren="1"><property name="CLASSNAME" type="string"><![CDATA[abc]]></property><property name="arr" fullname="$o->arr" facet="private" address="148282540" type="array" children="1" numchildren="2"></property></property></response>


    Which is obvious reason why it fails.
    I'm using latest SVN version (compiled today, 28 Nov 2009). I've tried it on 2.0.5 compiled with PECL as well.
    Both on Ubuntu Linux. Thread safe mode, 32 bit.
     
    elum.chaitu, Sep 6, 2010 IP
  2. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #2
    I didn't read your code, but private members are only available to base class (and cannot be access directly), it won't be available to derived classes. If you want it to be available to derived classes, you use protected instead of private.
     
    Rainulf, Sep 6, 2010 IP