Inheritance under the hood

So, this is the last question about annoying inheritance, which I had a little for me, so I wanted to continue and ask. Therefore, I will give an example in PHP:

<?php class Base { private $z = 4; function GetPrivate() { echo $this->z; } } class Derived extends Base { } $b = new Base(); $d = new Derived(); $d->GetPrivate(); 

? >

Simple enough. When I always read about inheritance, the explanation was simply β€œyou inherit the public and protected members” and what it is. I do not get a couple of things about how the interpreter in this example shows what belongs to what.

For example, when creating a derived class, I can use the public GetPrivate function of the database to get the private variables of the base class. However, a simple definition of inheritance does not work for me. I mean, I inherit the GetPrivate method, but I still have some kind of reference to private variables only from this method belonging to the base class (although $ this refers to an object of the derived class). I could not create a new Derived class function to access these private variables.

Thus, does the interpreter support tabs with inherited functions from the base class and possible relationships that they use for private members that are available only for this base class?

+6
inheritance php
source share
3 answers

An interpreter (or a compiler in another OOP language) checks access one step at a time.

When you call $d->GetPrivate(); The context check interpreter in this is the main one (public context, since I assume that you are not associated with the Drerived or Base class) and GetPrivate() is a public method, Thus, $d->GetPrivate(); allowed in this context, so no errors.

In GetPrivate() context is a $d object like Base , and access to z is a private element of the current object ( $d ). Therefore, access is valid.

The concept that plays here is Data Hiding (access control) and Encapsulation (a combination of data and functions).

Inheritance for playback is only to use GetPrivate() of Base , since it belongs to the Derived object.

It is true that there is still a link to personal data, but this link is not direct. The importance is that access occurs as a Base valid class .

So, to answer your question:

YES! The interpreter continues to monitor which inherited functions from the base class and the possible references they contain for private members that are available only for this base class.

Hope this helps.

+2
source share

The answer is simple: yes, try this:

 <?php class Base { private $z = 10; public function getPrivate() { return $this->z; } } class Derived extends Base { public function getPrivate() { return $this->z; } } $a = new Derived(); echo $a->getPrivate(); 

You will see that now that we have defined getPrivate in the Derived class, we can no longer access z in Base with its personal, if we want to access it from a derived class, we must declare it protected, not private.

+2
source share

Well, I can't talk much about the details of the parser, but the key to understanding is understanding what visibility means:

Class members declared public may be available worldwide. Access to elements declared protected can only be accessed within the class itself and the inherited and parent classes. Participants declared as private can only have access to the class that defines the member.

Now the PHP manual also says:

For example, when extending a class, a subclass inherits all public and protected methods from the parent class. If the class does not override these methods, they will retain their original functionality.

If you do var_dump($d) in a derived class, you will see that it contains Base->z :

 object(Derived)#2 (1) { ["z":"Base":private]=> int(4) } 

Thus, there is a link to z in the database, but it is private and, since it is private, a member can only be accessed by the class that defines the member, you cannot access it from Derived.

Offering public methods in the database for access to a private member, you effectively control access through the parent method. Perhaps $z is something that should be read-only in extended classes, such as a database adapter.

This is information hiding and access control. This does not mean that if you expand Base, you lose $ z. Inheritance is an is-a relationship. Derived is-a Base, and as such, it has $ z, although not by itself, but through it the parent.

+1
source share

All Articles