Inheritance in PHP

Inheritance is a concept in object oriented programming. With the help of inheritance we can get all property and method of one class in another class. Inheritance in php is introduced from php5 version.

Here I will explore about basics concept of inheritance  After basic we will discuss implementation of inheritance in php. 

Inheritance?

Inheritance is nothing but a design principle in oop. By implementing inheritance you can inherit(or get) all properties and methods of one class to another class. The class who inherit feature of another class known as child class. The class which is being inherited is know as parent class. Concept of the inheritance in oop is same as inheritance in real world. For example, child inherits characteristics  of their parent. Same is here in oop. One class is inheriting characteristics of another class.

With the help of inheritance you can increase re-usability of code. Let us take an example in terms of generic programming practices. Suppose you are going to create classes to render different html tag(div, span, form, table etc).  Now you will create class with name html_div, html_span ,  html_form. You are creating different class because every element is different in nature. For example form has action and method and you will have different input element in form. But table will have tbody, tr, th and td.

Now just think for some moment. There are some element and their rendering is same in all element. For example all html mention above is having name, id, class attribute which is same. Also rendering of those element is also same. So in above case you can create parent class with name tagsDetails and you can inherit that class across all of your html tags like div, span, form. Following is the generic code structure of inheritance in oop taking your HTML attribute in consideration.

Above code is and example of basic inheritance in php. All method(protected and public) from tagDetails class is directly accessible in your class page class. In child class you no need to write rendering of id and name logic again and again. This really saves time and give some good modulations in the code.

Hope your basic understanding about inheritance is clear. Now let us move to implementation of inheritance in php.

Multilevel and Multiple inheritance in PHP

In php multilevel inheritance is possible but multiple inheritance is not possible. In simplified terms in php child class can not inherit more than one parent class. But hierarchical inheritance is possible in php. Hierarchical means Parent inherit property of grand parent class. Grand child inherit property of parent class. So in multilevel inheritance child can get some property of from grand parent class also.

Example of Multiple inheritance in PHP

Above code will not work in php. Because php is single inheritance language.

Example of Multilevel inheritance in PHP

This is very basic example of multilevel inheritance. In php it is possible to implement multilevel inheritance. In above example parent class is inheriting grand parent property. And and child is inheriting parent property. So child have some parent and grand parent property.

Static Methods and Property in Inheritance in PHP

As in our example of page class we have explored that we can use $this-> keyword to get all property and method of parent(tagsDetails) class. But if your parent or child method is static, then you can access static methods or properties using self and parent keyword. Also this is not necessary to make method static if you want to use self or parent keyword. This is very useful if your parent and child both method is having property or method with same name. If both classes having same property and you want to call specific property or method then you can use this keyword.

Self and parent in case of static methods:

Self and Parent without static

 

Overriding in PHP

Method Overriding in OOP ?

Basic meaning of overriding in oop is same as real word meaning. In real word meaning of overriding  phenomena of replacing the same parental behaviour in child. This is same in case of method overriding in oop. In oop meaning of overriding is to replace parent class method in child class. Or in simple technical word method overriding mean changing behaviour of the method. In oop overriding is process by which you can re-declare your parent class method in child class. So basic meaning of overriding in oop is to change behaviour of your parent class method.

Normally method overriding required when your parent class have some method, but in your child class you want the same method with different behaviour. By overriding of method you can complete change its behaviour from parent class. To implement method overriding in oop we commonly create same method in child class.

Overriding in php is very easy. As we know that overriding is process of modifying the inherited method. So in case of inheritance you only need to create method with same name in your child class which you want to override. Following is example of overriding of method in php.

In above example you are overriding function f2. While overriding you are free to change business logic, visibility and number of parameter.

For more detail about overloading and overriding in php you can read:
http://php.net/manual/en/language.oop5.overloading.php

Overloading in PHP

Method Overloading in OOP ?

Overloading in oop is same as overloading in real word. In real word overloading means assigning extra work to same machine or person. In oop method overloading is same. By process of method overloading you are asking your method to some extra work. Or in some cases we can say some different work also.

Normally method overloading in oop is managed on the basis of the argument passed in function. We can achieve overloading in oop by providing different argument in same function.

Implementation of overriding can not be achieved by creating 2 function with same name and different argument in php. Because we can not create same name function more than 1 time in php class. To implement overloading we need to take help of magic method in php. In below section we will explore overloading.

Overloading in PHP

As we know that we can not implement overloading by create 2 function in with same name in class. So to implement overloading in php we will take help of magic method __call. Magic method __call invoked when method called by class object is not available in class. So here we will not create method exactly and will take help of __call method. Now call method will provide us 2 argument, 1st name of the method called and parameter of the function. Now with the help of either switch , case or if else we will implement overloading in php. Following is very simple example of overloading in php.

As in above class test magic method __call is implemented which is managing overloading

As we know that __call magic method invoked when method is not available in the class. So in case of above test class example we have not created function overlodedFunction. So whenever method overlodedFunction is called __call invoked. __call pass 2 variable, first  name of the called method and other is parameter passed in the called function.

Now in the __call function I have applied if condition to ensure that our business logic of overloading works only for overlodedFunction function. In if block we have counted number of argument in parameter and applied business logic.

Abstract Classes in PHP

Abstract classes are those classes which can not be directly initialised. Or in other word we can say that you can not create object of abstract classes. Abstract classes always created for inheritance purpose. You can only inherit abstract class in your child class. Lots of people say that in abstract class at least your one method should be abstract. Abstract method are the method which is only defined but declared. This is not true definition as per my assumption. But your any class has at least one method abstract than your class is abstract class.

Usually abstract class are also known as base class. We call it base class because abstract class are not the class which is available directly for creating object. It can only act as parent class of any normal class. You can use abstract class in class hierarchy. Mean one abstract class can inherit another abstract class also.

Abstract classes in PHP

Abstract classes in php are similar like other oop languages. You can create abstract classes in php using abstract keyword. Once you will make any class abstract in php you can not create object of that class.

above code will throw error in php.

Abstract classes in php are only for inheriting in other class.

In above example you are creating of testChild Class. TestChild class is inheriting testParent abstract class. So your abstract class is only available for inheritance. Main motive of creating abstract classes in php is to apply restriction of direct initialization or object creation.

Implementation of abstract method

As we know that abstract functions are those functions of abstract class which is only defined. It will be declared in your child class. You can create any method abstract using keyword abstract. You can only create abstract method either in abstract class or interface. Following is example of the abstract method implementation:

In class abc we have defined an abstract function f1. Now when we have inherited class abc then declared function f1. If you have an abstract method in your abstract class then once you inherit your abstract class then it is necessary to declare your abstract method. If you will not declare your abstract method then PHP will throw error in that case.

You can declare your abstract method in child class with the same visibility or less restricted visibility.

In above code you can see that you have declare 3 function in abstract class. But private declaration of the abstract method will always throw error. Because private method is available only in the same class context. But in case of f1. This is protected. Now in child class we have defined it as public because public is less restricted than protected. And for function f2 which is already public so we have defined it as public in our child class. We have defined it public because no any visibility is less restricted than public.

Interface in PHP

Interface

Interface in oop enforce definition of some set of method in the class. By implementing interface you are forcing any class to must declaring some specific set of methods in oop. For example if you are creating class to render HTML element then it is necessary to set id and name of your html tag. So in this case you will create interface for that class and define method like setID and setName. So whenever someone will create any class to render HTML tag and implemented your interface then he must need to define setId and setName method in their class. In other word you can say that by help of interface you can set some definition of your object. Interface is very useful if you are creating architecture of any oop base application.

Interface in PHP

Interface in php can be implemented like other oop lanugage. You can create interface in php using keyword interface. By implementation of interface in php class you are specifying set of the method which classes must implement.

You can create interface in php using interface keyword. Rest of the things are typically identical to classes. Following is very small example of interface in php.

So in above code you are creating interface with name abc. Interface abc has function xyz. Whenever you will implement abc interface in your class then you have to create method with name xyz. If you will not create function xyz then it will throw error.

You can implement your interface in your class using implements keyword. Let us implement our interface abc in our class

You can only define method in interface with public accessibility. If you will use other than public visibility in interface then it will throw error. Also while defining method in your interface do not use abstract keyword in your methods.

You can also extend interface like class. You can extend interface in php using extends keyword.

So here template2 has all property of tempate2. So whenever you will implement template2 in your class, you have to create function of both interfaces.

You can also extend multiple interface in one interface in php.

You can also implement more than one interface in php class.

You can not implement 2 interfaces if both share function with same name. It will throw error.

Your function parameter in class must be identical to the parameter in the interface signature. Following is example some example

Above will work. But following example will not work:

But it is not necessary to use the same name of the variable. Like $a. You can also use any name. For example:

If  you are using default argument then you can change your value of the argument. For example