Thursday, January 25, 2018

Types of Inheritance in PHP


Recall Mindi’s tattoos, specifically the tattoo on her back? The tattoo on her back was described as protected in PHP terms meaning that it was visible within the Mindi class and to classes that extended the Mindi class. To inherit in PHP5, you should use the keyword extends in the class definition.

Class parent


Class child extends parent



Inheritance passes "knowledge" down. It is a structured way of extending code and functionality into new programs and packages. Classes are created in hierarchies and inheritance allows the structure and methods in one class to be passed down the hierarchy. That means less programming is required when adding functions to complex systems. If a step is added at the bottom of a hierarchy, then only the processing and data associated with that unique step needs to be added. Everything else about that step is inherited. The ability to reuse existing objects is considered a major advantage of object oriented programming.

◆.    Method Overriding 

Method overriding is when the function of base class is re-defined with the same name, function signature and access modifier (either public or protected) of the derived class.

The reason to override a method is to provide additional functionality over and above what has been defined in the base class. Imagine that you have a class by the name of Beverage from which you derive two child classes, Martini and Scotch. The Beverage class has methods defined to stir, mix, etc, but each of the specialized classes Martini and Scotch will have its own style of mixing and stirring and hence would need to override the stir and mix functionality.

Lets look at an example with Beverage:

◆.   Invoking Parent Methods 

First of all, “invoke” means to call, to request the action of something. I just wanted to get that brief explanation out of the way. Now, when you override a method of the base class, its functionality is completely hidden unless it has been explicitly invoked from the child class. To invoke a parent class method you should use the keyword parent followed by the scope resolution operator “:: “ followed by the name of the method as mentioned below:

Parent : : function_name();

Look at the example below: 

In the above example, look at the way in which the showData() function in the Employee child class is invoking the Person parent class’s showData() function. When the program executes the showData() method if the Employee class is called which in turn calls the showData() function of the parent class. After the parent class’s showData() function completes its execution the remaining code in showData() function of the Employee class is executed.

◆.   Horizontal Inheritance – Using Traits 

PHP does not support multiple inheritance; a class can only extend a single base class. What happens if you need code from more than one class? Until recently, developers had to accomplish this in a crude manner by copying and pasting code from additional classes that was needed outside of its base class. Now this can be done by way of a new feature introduced in PHP5.4 called a trait that provides a means of horizontal inheritance.

A trait looks like a class in the way it is structured but it cannot be instantiated. Properties and methods can be defined within a trait structure but they can only be used by a class that incorporates them with the use keyword which is called within the class that uses it, that is, it goes inside the curly braces. It isn’t called in the class definition outside the curly braces like interfaces and the extending class.

◆.    Encapsulation

Encapsulation means what the term implies, to put in a “capsule,” to place functionality into a single package. Consider an object as a capsule or a box that has things in it. The access modifiers are like windows that control who gets to see inside with ‘public’ access being a wide open window that let's everyone see inside and ‘private’ access being fully shaded that doesn't let anyone see inside.

The control over the visibility of the contents of an object is a big part of how encapsulation works. The contents of an object are properties and methods and because they are all packaged inside an object they make up a sort of mini-program that has control over how visible its contents, data, are. This is encapsulation.

Technically speaking, encapsulation refers to the creation of self-contained modules that bind processing functions to the data. Encapsulation ensures good code modularity, which keeps routines separate and less prone to conflict with each other. To reiterate, you can think of each object as a mini program that handles it’s own responsibilities as it’s own self contained capsule or pill.

◆.    Polymorphism

Now that you know that a class is a blueprint and that an object is constructed based on that class it is important to understand that many different objects can be created from the same class. I may have a single class for a table but I can create many different kinds of table objects from that class.

I could have a round table, a square table, a long table a short table. I could have a blue table or a brown table. I could have a table made of oak or one made from plastic; all from the same base class.

Polymorphism simply means many forms. In computer programming, polymorphism is the ability to create a variable, a function, or an object that has more than one form in order to provide capability of adapting to what needs to be done. It is a mechanism by which objects of different types can process data through a single interface. Polymorphism is used to make applications more modular and extensible. Instead of messy, hard to read conditional statements describing different courses of action, you create interchangeable objects that you select based on what you need. That is the fundamental goal of polymorphism.

Polymorphism is a mechanism by which objects of different types can process data through a single interface. Using a typical illustrative example, a program might define a type of object called Animal, containing a function called speak() and, through inheritance, derive object sub-types such as Cow, Dog, etc. that each have their own specific implementation of the speak() function. The mechanism of polymorphism allows such a program to call the speak() function without actually knowing whether it is calling it for a Cow, Dog, or any other derivative Animal, while executing the correct function for the specific type of Animal.

Polymorphism is not the same as method overloading or method overriding (in OOP, a method is a function that belongs to a class, while the class variables are referred to as its members) and should not be confused with these. Read More


Please comment and follow this site blog inbox