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: