Re: Re: Re: Ambiguity in the oop5 visibility docs

From: Date: Tue, 29 May 2018 11:40:51 +0000
Subject: Re: Re: Re: Ambiguity in the oop5 visibility docs
References: 1 2  Groups: php.doc 
Request: Send a blank email to phpdoc+get-969386959@lists.php.net to get a copy of this message
On 27 May 2018 at 19:02, Andrew Gromov <andrewgrom@rambler.ru> wrote: > class A { > public function foo(){ > echo $this->b; // No property $b in parent class > } > } > > class B extends A { > protected $b = 'foo'; > } > > $a = new B(); > $a->foo(); // prints 'foo' > > In classic OOP, parent class can't access unknown to him member of child > class. > In PHP, classes don't really "know" anything about their structure - everything is looked up dynamically at run-time. At run-time, there is a property called "b" on the instance, and it is visible in the current scope, so the access is allowed. You mention "classic OOP", but I'm not aware of any formal definition we could reference here, only precedent from other languages. Most languages don't need to worry about this case, because the reference to $this->b would fail to compile unless b was declared; but PHP can't raise that error, because accessing a completely undeclared property is allowed. > And phrases like > > > can be accessed within the class itself and parent classes. > > explode the brain! How it can be - reverse inheritance, from child to parent!? > > I discussed a common use case for this in my first response on this thread: https://marc.info/?l=phpdoc&m=152727049709620&w=2 There is no *inheritance* from child to parent, just access from code written in a parent class to polymorphic behaviour in the child class. Regards, -- Rowan Collins [IMSoP]

« previous php.doc (#969386959) next »