< PHP Programming

Overloading

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

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.

ScopeReturn TypeMethodParamsSyntaxExtra Note
publicvoid__setstring $name , mixed $valuepublic void __set ( string $name , mixed $value )run when writing data to inaccessible properties.
publicmixed__getstring $namepublic mixed __get ( string $name )utilized for reading data from inaccessible properties
publicbool__issetstring $namepublic bool __isset ( string $name )triggered by calling isset() or empty() on inaccessible properties
publicvoid__unsetstring $namepublic void __unset ( string $name )invoked when unset() is used on inaccessible properties.

Method overloading

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

SyntaxDescription
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

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.

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.