Re: Bazar behavior w/ private member variables

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Jul 12, 2012 at 9:23 PM, Nathan Nobbe <quickshiftin@xxxxxxxxx>wrote:

> On Thu, Jul 12, 2012 at 8:38 PM, Tommy Pham <tommyhp2@xxxxxxxxx> wrote:
>
>> On Thu, Jul 12, 2012 at 7:19 PM, Nathan Nobbe <quickshiftin@xxxxxxxxx>
>> wrote:
>> > Hi all,
>> >
>> > Strangely PHP seems to let each class have its own layer of private
>> scope
>> > for member variables.  If a subclass defines a member variable of the
>> same
>> > name as one defined in the parent the values are maintained
>> independently
>> > in instances of the  child class.
>> >
>> > First off a simple class with a private member variable $_myPrivate,
>> and a
>> > public accessor method which returns its value:
>> >
>> > class A
>> > {
>> >     private $_myPrivate = 5;
>> >
>> >     public function getMyPrivate()
>> >     {
>> >         return $this->_myPrivate;
>> >     }
>> > }
>> >
>> >
>> > Second, a subclass, that gets weird right away, first we define a
>> private
>> > member variable that already has been defined in the parent class, and
>> give
>> > it a different initial value.  To illustrate the behavior we have two
>> > accessor methods, setMyPrivate that uses the $this keyword to get the
>> value
>> > of $_myPrivate, which returns the value of the subclasse's version of
>> the
>> > variable, and getParentsMyPrivate, that calls A::getMyPrivate via the
>> > parent keyword and it returns the value of $_myPrivate as defined in the
>> > base class.
>> >
>> > class B extends A
>> > {
>> >     private $_myPrivate = 6;
>> >
>> >     public function setMyPrivate()
>> >     {
>> >         $this->_myPrivate = 6;
>> >     }
>> >
>> >     public function getMyPrivate()
>> >     {
>> >         return $this->_myPrivate;
>> >     }
>> >
>> >     public function getParentsMyPrivate()
>> >     {
>> >         return parent::getMyPrivate();
>> >     }
>> > }
>> >
>> >
>> > Look at a var_dump of an instance of B:
>> >
>> > object(B)#2 (2) {
>> >   ["_myPrivate":"B":private]=>
>> >   int(6)
>> >   ["_myPrivate":"A":private]=>
>> >   int(5)
>> > }
>> >
>> > clearly storage is allocated for two different values.  Now I'm sure you
>> > all know that if I were to define a private method in A and try to call
>> it
>> > from B a Fatal error is raised, something on the order of
>> >
>> > PHP Fatal error:  Call to private method A::tryToCallMeFromB() from
>> context
>> > 'B'
>> >
>> > so why the special treatment for member variables, is this supposed to
>> be a
>> > feature?
>> >
>> > -nathan
>>
>> That is OOP accross all languages.  If you want the child class to
>> modify the variable, then set it to protected.  Private is only
>> accessible within that class.
>>
>
> I know that sounds like it should make sense but if it's true, it's an
> aspect I've never known about, at least maybe I'm just spacing really bad
> or something...
>
> Anyway, this chokes in javac:
>
> public class PrivateAccess
> {
>     private Boolean isAccessible = true;
> }
>
> class PrivateAccessChild extends PrivateAccess
> {
>     public Boolean getAccessible()
>     {
>         return isAccessible;
>     }
> }
>
> PrivateAccessChild.java:5: isAccessible has private access in PrivateAccess
>         return isAccessible;
>                ^
>
> -nathan
>

Ahhh, but if I add the private declaration in the subclass it works.  Where
have I been??

-nathan

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux