Overloading and Overriding in PHP

Overloading and Overriding in PHP with Example

Function or method Overloading and Overriding strategy is exceptionally essential and valuable component of any oop language. In this instructional exercise we will examine usage of strategy overloading and overriding in php. Here first we will investigate nuts and bolts of overloading and overriding. After investigation of nuts and bolts we will actualize overloading and overriding in php. Before going further I am expecting that you have essential information of classes and legacy in php. Additionally you have understanding about magic method in php. Magic Method in light of the fact that overloading in php can be implmented utilizing Magic Methods.

Method Overriding in OOP

Essential importance of overriding in oop is same as genuine word meaning. In genuine word importance of overriding marvels of supplanting the same parental conduct in child. This is same if there should be an occurrence of strategy overriding in oop. In oop importance of overriding is to supplant parent class strategy in child class. On the other hand in straightforward specialized word strategy overriding mean changing conduct of the technique. In oop overriding is process by which you can re-declare your parent class strategy in child class. So fundamental importance of overriding in oop is to change conduct of your parent class strategy.

Typically method overriding required when your parent class have some technique, however in your child class you need a similar technique with various conduct. By overriding of technique you can finish change its conduct from parent class. To implment method overiding in oop we generally make same technique in child class.

Method Overloading in OOP

Overloading in oop is same as Overloading in genuine word. In genuine word Overloading implies appointing additional work to same machine or individual. In oop strategy Overloading is same. By procedure of method Overloading you are asking your strategy to some additional work. On the other hand sometimes we can state some unique work too.

Typically method overloading in oop is overseen on the premise of the contention go in capacity. We can accomplish overloading in oop by giving different argument in same function.

Overloading and Overriding in PHP

Hope your fundamental idea of overloading and overriding is clear at this point. Presently let us investigate execution of overloading and overriding in php.

Execution of overriding in php is simple. On the off chance that your parent class has a function. You can create function with same name in your child class to override the function. Execution of overriding can not be accomplished by making 2 functions with same name and different argument in php. Since we can not make same name function more than 1 time in php class. To execute overloading we have to take help of magic method in php. In underneath section we will investigate overloading and overriding one by one.

Example of Overloading

As we realize that we can not actualize overloading by make 2 function in with same name in class. So to execute overloading in php we will take help of magic method __call. magic method __call conjured when strategy called by class object is not accessible in class. So here we won’t make method precisely and will take help of __call strategy. Presently call strategy will give us 2 argument, first name of the method called and parameter of the function. Presently with the assistance of either switch case or if else we will actualize overloading in php. Taking after is extremely basic case of over-burdening in php.

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

As we realize that __call magic method summoned when technique is not accessible in the class. So if there should arise an occurrence of above test class case we have not made capacity overlodedFun. So at whatever point method overlodedFun is called __call summoned. __call pass 2 variable, first name of the called method and other is parameter passed in the called function.

Presently in the __call work I have connected if condition to guarantee that our business rationale of overloading works just for overlodedFun work. In if block we have counted number of argument in parameter and connected business rationale.

Example of Overriding

Overriding in php is simple. As we realize that Overriding is procedure of altering the inherited method. So if there should be an occurrence of inheritance you just need to make method with same name in your child class which you need to override. Following is example of overriding of method in php.

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

I hope your concept of the overloading and overriding in php is clear now.Subscribe me and do share this post 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *