Re: private properties ....

From: Date: Sat, 01 Dec 2007 15:41:47 +0000
Subject: Re: private properties ....
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-33569@lists.php.net to get a copy of this message
Jingcheng Zhang 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 > $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 > } > } > ?> > Access modifiers aren't for specifying access that "instances" have to members, it is for specifying access that "classes" have to members. Instances != Classes. The behavior you want above can be done just as easily just using protected members and I don't see how using protected members would hurt you. > Thanks. > > > On Dec 1, 2007 12:57 AM, Johannes Schlüter <johannes@php.net> 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 >> >> >> > > >

« previous php.internals (#33569) next »