PHP Programming/Overriding and Overloading

Overloading edit

Overloading in PHP provides means to dynamically "create" properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types. In other terms creating properties/methods at run-time is called property overloading/method overloading.

Property overloading edit

In PHP, property overloading can be done by magic methods like __set, __unset, __isset, __get method. Overloading can be done by magic methods like __call and __call_static. PHP's interpretation of "overloading" is different than most object oriented languages. Overloading traditionally provides the ability to have multiple methods with the same name but different quantities and types of arguments.

Scope Return Type Method Params Syntax Extra Note
public void __set string $name , mixed $value public void __set ( string $name , mixed $value ) run when writing data to inaccessible properties.
public mixed __get string $name public mixed __get ( string $name ) utilized for reading data from inaccessible properties
public bool __isset string $name public bool __isset ( string $name ) triggered by calling isset() or empty() on inaccessible properties
public void __unset string $name public void __unset ( string $name ) invoked when unset() is used on inaccessible properties.

Method overloading edit

When we try to call a method that doesn't exist in PHP, __call() is called and that way we achieve Method Overloading.

Syntax Description
public mixed __call ( string $name , array $arguments ) __call() is triggered when invoking inaccessible methods in an object context.
public static mixed __callStatic ( string $name , array $arguments ) __callStatic() is triggered when invoking inaccessible methods in a static context.

Overriding edit

In OOPs meaning of overriding is to replace parent class method in child class. Or in simple technical word method overriding mean changing behavior 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 behavior 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 behavior. By overriding of method you can completely change its behavior from parent class. To implement method overriding in OOP we commonly create same method in child class.