Function overloading in PHP is used to dynamically create properties and methods. These dynamic objects are processed by magic methods that can be used in the class for various types of actions. Function overloading contains the same function name, and this function performs different tasks depending on the number of arguments. For example, find an area of certain shapes where the radius is given, then it should return the area of the circle, if the height and width are specified, then it should give the area of the rectangle-method/">rectangle and others. Like other OOP languages, function overloading cannot be done natively. In PHP, function overloading is done using the __call() magic function. This function takes a function name and arguments.Property and overloading rules in PHP:
- All overloading methods must be defined as Public.
- After creating an object for a class, we can access a collection of entities that are properties or methods not defined in the scope of the class.
- Such objects are called overloaded properties or methods, and the process is called overloading .
- PHP magic methods are used to manipulate these overloaded properties or functions.
- Most magic methods will run in the context of an object, except for the __callStatic() method, which is used in a static context.
- Property overloading
- Method overloading
The following operations are performed on overloaded properties in PHP.
- Setting and getting overloaded properties.
- Evaluating overloaded property settings.
- Cancel the setting such properties.
- __set():fires when overloaded properties are initialized.
- __get():fires when used overloaded properties with PHP print statements.
- __isset():this magic method is called when we check overloaded properties with isset()
- __ unset():Likewise, this function will be called when PHP unset() is used for overloaded properties.
//
class
GFG {
// Data overload location
private
$data
=
array
();
// No overloading is used here
public
$declared
= 1;
// Overload is used on access
// outside the class
private
$hidden
= 2;
// Function definition
public
function
__ set (
$name
,
$value
) {
echo
" Setting ’$name’ to’ $value’ "
;
$this
-> data [
$name
] =
$value
;
}
// Function definition
public
function
__ get (
$name
) {
echo
"Getting’ $name: "
;
if
(
array_key_exists
(
$name
,
$this
-> data)) {
return
$this
-> data [
$name
];
}
$trace
= debug_backtrace();
return
null;
}
// Function definition
public
function
__ isset (
$name
) {
echo
"Is’ $name’ set? "
;
return
isset ( $this
-> data [
$name
]);
}
// __unset function definition
public
function
__ unset (
$name
) {
echo
"Unsetting’ $name’ "
;
unset (
$this -> data [
$name
]);
}
// getHidden function definition
public
function
getHidden ( ) {
return
$this
-> hidden;
}
}
// Create object
$obj
=
new
GFG;
// Set object variable to 1
$obj
-> a = 1;
echo
$obj
-> a ...
""
;
// Use the isset function to check
// "a" is set or not
var_dump (isset (
$obj
-> a));
// Reset ’ a ’
unset (
$obj
-> a);
var_dump (isset (
$obj
-> a));
echo
$obj
-> declared.
""
;
echo
" Private property are visible inside the class "
;
echo
$obj
-> getHidden().
""
;
echo
" Private property are not visible outside of class "
;
echo
$obj
-> hidden ...
""
;
?>
Exit: Setting ’a ’to’ 1’ Getting ’a: 1 Is’ a’ set? bool (true) Unsetting ’a’ Is’ a’ set? bool (false) 1 Private property are visible inside the class 2 Private property are not visible outside of class Getting ’hidden:
Method overloading:is an overload type for creating dynamic methods, which are not declared in class scope. PHP method overloading also runs magic methods designed for a specific purpose. Unlike property overloading, PHP method overloading allows you to call functions on both an object and a static context.
Related magic functions, - __call() & # 8212; fires when overloaded methods are called in the context of an object.
- __callStatic() & # 8212; fires when overloaded methods are called in a static context.
Example : class
GFG {
public
function
__ call (
$name
,
$arguments
) {
echo
"Calling object method’ $name’ "
. implode (
’,’
,
$arguments
).
""
;
}
public
static
function
__ callStatic (
$name
,
$arguments
) {
echo
" Calling static method ’$name’"
. implode (
’,’
,
$arguments
).
""
;
}
}
// Create a new object
$obj
=
new
GFG;
$obj
-> runTest (
’ in object context’
);
GFG::runTest (
’in static context’
);
?>
Exit: Calling object method ’runTest’ in object context Calling static method’ runTest’ in static context