Home  >  Article  >  Backend Development  >  Introduction to overloading in PHP

Introduction to overloading in PHP

小云云
小云云Original
2018-03-20 13:46:101638browse

The "overloading" provided by PHP refers to dynamically "creating" class attributes and methods. We do this through magic methods. Overloaded methods are called when a class property or method is called that is undefined or inaccessible in the current environment. All overloaded methods must be declared public.

**note:** "Overloading" in PHP is different from most other object-oriented languages. Traditional "overloading" is used to provide multiple class methods with the same name, but each method has different parameter types and numbers.

1. Property overloading

public void __set ( string $name , mixed $value )
public mixed __get ( string $name )
public bool __isset ( string $name )
public void __unset ( string $name )

When assigning a value to an inaccessible property, __set() will be called.
When reading the value of an inaccessible attribute, __get() will be called.
When isset() or empty() is called on an inaccessible property, __isset() will be called.
When unset() is called on an inaccessible property, __unset() will be called.
The parameter $name refers to the name of the variable to be operated. The value of the

#name variable of the __set() method.
Attribute overloading can only be done in objects. In static methods, these magic methods will not be called. So none of these methods can be declared static. As of PHP 5.3.0, defining these magic methods as static produces a warning.

2. Method overloading

public mixed __call ( string $name , array $arguments )
public static mixed __callStatic ( string $name , array $arguments )

When an inaccessible method is called in an object, __call() will be called.
When calling an inaccessible method in a static context, __callStatic() will be called.

arguments parameter is an enumeration array containing the parameters to be passed to the method $name.

The "overloading" provided by PHP refers to dynamically "creating" class attributes and methods. We do this through magic methods.
The overloaded method will be called when a class attribute or method that is undefined or inaccessible in the current environment is called.
All overloaded methods must be declared public.
**note:** "Overloading" in PHP is different from most other object-oriented languages. Traditional "overloading" is used to provide multiple class methods with the same name, but each method has different parameter types and numbers.

Related recommendations:

php overloaded array operator_PHP tutorial

php overloaded array operator

Application occasions of method overloading (overwriting) in php inheritance, php overloading

The above is the detailed content of Introduction to overloading in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn