PHP provides a "reload" (overloading) that refers to the dynamic "create" class properties and methods. We do this by means of magic methods.
Overloaded methods are called when a class property or method is called that is undefined or not visible under the current environment.
All overloaded methods must be declared public.
Attention:
1) The parameters of these magic methods cannot be passed by reference.
2) "Overloading" in PHP is different from most other object-oriented languages. Traditional "overloads" are used to provide multiple class methods with the same name, but each method has a different parameter type and number.
Property overloading
Public Void__set (String$name, Mixed$value) publicmixed__get (string$name) publicbool__isset (string$name) publicvoid_ _unset (String$name)
__set () is called when a value is assigned to an unreachable property.
__get () is called when the value of an inaccessible property is read.
When Isset () or empty () is called on an inaccessible property, __isset () is called.
When Unset () is called on an inaccessible property, __unset () is called.
The parameter $name refers to the name of the variable to manipulate. The $value parameter of the __set () method specifies the value of the $name variable.
Property overloading can only be done in an object. In static methods, these magic methods will not be called. So none of these methods can be declared static. Starting with PHP 5.3.0, defining these magic methods as static generates a warning.
Attention:
Because of how PHP handles assignment operations, the return value of __set () is ignored. Similarly, in the chain assignment below, __get () is not called:
$a = $obj->b = 8;
Attention:
Overloaded properties cannot be used in other language constructs other than isset (), which means that overloaded magic methods will not be called when empty () is used on an overloaded property.
To circumvent this restriction, you must assign an overloaded property to a local variable and then use empty ().
Example #1 using __get (), __set (), __isset (), and __unset () for property overloading
Class propertytest{/* The overloaded data is saved in this */private $data = Array (); /* Overloads cannot be used on properties that have already been defined */public $declared = 1; /* The overload will only occur if this property is accessed from outside the class */private $hidden = 2; Public Function __set ($name, $value) {echo "Setting ' $name ' to ' $value ' <br>"; $this->data[$name] = $value; } Public Function __get ($name) {echo "Getting ' $name ' <br>"; if (Array_key_exists ($name, $this->data)) {return $this->data[$name]; } $trace = Debug_backtrace (); Trigger_error (' unknown attribute via __get (): '. $name. ' in '. $trace [0][' file ']. $trace [0][' line '], e_user_notice); return null; The version after/*php5.1.0 */Public Function __isset ($name) {echo "is ' $name ' set?<br>"; return Isset ($this->data[$name]); The version after/*php5.1.0 */Public Function __unset ($name) {echo "unsetting ' $name ' <br>"; unset ($this->data[$name]); }/* Non-magic method */Public function GethIdden () {return $this->hidden; }}echo ' <pre> '; $obj = new PropertyTest; $obj->a = 1;echo $obj->a. ' <br> '; Var_dump (Isset ($obj->a)); unset ($obj->a); Echo ' <br> '; echo $obj->declared. ' <br> '; Echo ' let's experiment with the private property named ' Hidden ':<br> "; echo" privates is visible inside The Class,so __set () not used...<br> "; Echo $obj->gethidden (). ' <br> '; Echo ' privates not visible outside of Class,so __get () is used...<br> "; Echo $obj->hidden;
Output Result:
Setting ' A ' to ' 1 '
Getting ' a '
1
Is ' a ' set?
BOOL (TRUE)
Unsetting ' a '
1
Let's experiment with the private property named ' Hidden ':
Privates is visible inside the Class,so __set () not used ...
2
Privates not visible outside of Class,so __get () is used ...
Getting ' hidden '
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 () is called.
When a non-accessible method is called in a static manner, __callstatic () is called.
The $name parameter is the name of the method to invoke. The $arguments parameter is an enumerated array that contains the arguments to pass to the method $name.
Example #2 using __call () and __callstatic () to method overloading
Class methodtest{public function __call ($name, $arguments) { //Note: $name value is case sensitive echo "calling Object Method ' $name ' ". Implode (', ', $argument). ' <br> '; } PHP5.3.0 version public static function __callstatic ($name, $arguments) { //Note: The value of $name is case-sensitive echo " Calling static method ' $name ' ". Implode (', ', $argument). ' <br> '; }} $obj = new Methodtest; $obj-Runtest (' in object context '); Methodtest::runtest (' in static context ');
Output Result:
Calling object method ' Runtest '
Calling object method ' Runtest '