Overloading
Both method calls and member accesses can be overloaded via the
__call, __get and __set methods. These methods will only be
triggered when your object or inherited object doesn't contain the
member or method you're trying to access.
All overloading methods must not be defined as
static.
All overloading methods must be defined as
public.
Since PHP 5.1.0 it is also possible to overload the
isset and unset functions via the
__isset and __unset methods respectively.
Method __isset is called also with empty.
Member overloadingvoid__setstringnamemixedvaluemixed__getstringnamebool__issetstringnamevoid__unsetstringname
Class members can be overloaded to run custom code defined in your class
by defining these specially named methods. The $name
parameter used is the name of the variable that should be set or retrieved.
The __set() method's $value parameter specifies the
value that the object should set the $name.
overloading with __get, __set, __isset and __unset example
1, "b" => 2, "c" => 3);
public function __get($nm)
{
echo "Getting [$nm]\n";
if (isset($this->x[$nm])) {
$r = $this->x[$nm];
print "Returning: $r\n";
return $r;
} else {
echo "Nothing!\n";
}
}
public function __set($nm, $val)
{
echo "Setting [$nm] to $val\n";
if (isset($this->x[$nm])) {
$this->x[$nm] = $val;
echo "OK!\n";
} else {
echo "Not OK!\n";
}
}
public function __isset($nm)
{
echo "Checking if $nm is set\n";
return isset($this->x[$nm]);
}
public function __unset($nm)
{
echo "Unsetting $nm\n";
unset($this->x[$nm]);
}
}
$foo = new Setter();
$foo->n = 1;
$foo->a = 100;
$foo->a++;
$foo->z++;
var_dump(isset($foo->a)); //true
unset($foo->a);
var_dump(isset($foo->a)); //false
// this doesn't pass through the __isset() method
// because 'n' is a public property
var_dump(isset($foo->n));
var_dump($foo);
?>
]]>
&example.outputs;
int(1)
["x:private"]=>
array(2) {
["b"]=>
int(2)
["c"]=>
int(3)
}
}
]]>
Method overloadingmixed__callstringnamearrayarguments
The magic method __call() allows to capture invocation of non existing
methods. That way __call() can be used to implement user defined method
handling that depends on the name of the actual method being called. This
is for instance useful for proxy implementations. The arguments that were
passed in the function will be defined as an array in the
$arguments parameter. The value returned from the
__call() method will be returned to the caller of the method.
overloading with __call example
x;
}
}
$foo = new Caller();
$a = $foo->test(1, "2", 3.4, true);
var_dump($a);
?>
]]>
&example.outputs;
int(1)
[1]=>
string(1) "2"
[2]=>
float(3.4)
[3]=>
bool(true)
}
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
]]>