Calling magic
PHP 5 includes a number of magic methods that a class can implement. Each magic method begins with __, and gets called automatically by PHP under certain conditions. Technically some are called overloading methods rather than magic methods, but they all effectively work by the same magic blue smoke.
Today we're just going to look at one of them: __call(). If a class implements __call(), then if an object of that class is called with a method that doesn't exist __call() is called instead. To wit:
<?php
class Caller {
private $x = array('a', 'b', 'c');
public function __call($method, $args) {
print "Method $method called:\n";
var_dump($args);
return $this->x;
}
}
$foo = new Caller();
$a = $foo->test(1, 2, 3);
var_dump($a);
?>
Комментариев нет:
Отправить комментарий