Hi Etienne,
  Is "private" only an access limiter between classes?If so, I think private
properties and methods should be OK to be extended into the child class, but
currently that's not the case, and there is also a bug here, consider the
following example:

<?php
class P {
    private $name = 'hello';
}
class C extends P {
    public function f() {
        echo $this->name;
    }
}
$o = new C();
var_dump($o); // object(C)#1 (1) { ["name:private"]=> string(5) "hello" }
$o->f();      // Notice: Undefined property: C::$name in
D:\Development\Workspace\index.php on line 7
?>

When using var_dump on $o, it shouldn't display the private property "name"
here as it is not extended. If we allow private properties be extended into
the child class, then the codes above and following should both  work OK:

<?php
class P {
    private $name = 'hello';
}
class C extends P {
    public function f() {
        $o = new P();
        echo $o->name;    // Incorrect, as the code scope is not class P
    }
    public function g() {
        echo $this->name; // This should be OK, as $name is extended from P,
and is property of class C, not class P
    }
}
$c = new C();
$c->f();
$c->g();
?>


On Nov 30, 2007 10:08 PM, Etienne Kneuss <[EMAIL PROTECTED]> wrote:

> Hello,
>
> On 11/30/07, Marco Kaiser <[EMAIL PROTECTED]> wrote:
> >
> > Conclusion:
> > 1. Why i can access a private property from a different class instance
> > (name) but same type ?
> > $aa and $bb are instances of aaa but not the same.
> > 2. This doesnt works if cc is a own class with same property name (ie.
> > interface or something like this)
>
>
> The check whether a class property/method is accessible is based on the
> class of the scope you're in. It's not based on the instance. Note that
> encapsulation is still conserved as private properties are accessible only
> from methods of the class they're defined in. It also allows you to work
> with static methods that access private properties.
>
> 3. Is it a bug that i can't use same property name in my child class?
> > (normaly the parent property isnt visible to the child)
> > cc extends aaa.
>
>
> I'm not sure what you mean. As you can do:
>
> class A { private $foo = 2; }
> class B extends A { private $foo = 3; public function foo() { echo
> $this->foo; } }
> $b = new B; $b->foo(); // 3
>
>
> Thats just some questions :)
> >
> > -- Marco
> >
> > --
> > PHP Internals - PHP Runtime Development Mailing List
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> >
>
>
> --
> Etienne Kneuss
> http://www.colder.ch
>
> Men never do evil so completely and cheerfully as
> when they do it from a religious conviction.
> -- Pascal
>



-- 
Best regards,
Jingcheng Zhang
Room 304, Dormitory 26 of Yuquan Campus, Zhejiang University
P.R.China

Reply via email to