On 01/12/2007, Jingcheng Zhang <[EMAIL PROTECTED]> wrote:
> Well, yes, "private" denies accessing from other class(including its child
> class), this is what "encapsulation" means. But when refering to
> inheritance, why forbids private properties/methods being *extended* to
> child classes? This is what I mean, as the following example:
>
> <?php
> class p {
>     protected $foo = 'foo';
>     private   $bar = 'bar';
> }
> class c extends p {
>     public function f() {
>         $o = new p();
>         $o->foo = 'FOO';    // Correct, "foo" is protected property of p and
> thus allow accessing from child class

This is not allowed. $o is in a different context (I think that's the
right way of describing this) and as such has no access to the private
or protected members of $o. Just because it $o is created within class
c, it is not related.

>         $o->bar = 'BAR';    // Incorrect, "bar" is private property of
> *class p* here
>     }
>     public function g() {
>         $this->foo = 'FOO'; // Correct, "foo" is
>         $this->bar = 'BAR'; // Should be OK, as "bar" is private property of
> *class c* here

Private means only of use in the class in which is created, not
sub-classes or public space.
>     }
> }
> ?>
>
> Thanks.
>
>
> On Dec 1, 2007 12:57 AM, Johannes Schlüter <[EMAIL PROTECTED]> wrote:
>
> > Hi,
> >
> > On Sat, 2007-12-01 at 00:24 +0800, "Jingcheng Zhang" wrote:
> > > 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:
> >
> > The child class is another class, "private" gives you encapsulation
> > inside the base class's context. and preventing acces from other class's
> > context.
> >
> > johannes
> >
> >
>
>
> --
> Best regards,
> Jingcheng Zhang
> Room 304, Dormitory 26 of Yuquan Campus, Zhejiang University
> P.R.China
>


-- 
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

Reply via email to