Php provides built-in interceptor methods.This also known as overloading but since that term means something quite different in Java and C++. PHP5 supports three built-in interceptor methods.Like __construct(), these are invoked for you when the right conditions are met.
Below the tables describes this methods.
| Method | Description |
|---|---|
| __get($property) | Invoked when an undefined property is accessed |
| __set($property) | Invoked when a value is assigned to an undefined property |
| __isset($property) | Invoked when isset() is called on an undefined property |
| __unset($property) | Invoked when unset() is called on an undefined property |
| __call($method, $arg_array) | Invoked when an undefined method is called |
(PHP Objects, Patterns, and Practice Book - Matt Zandstra - Apress. Page 60.)
We touch on __call() method for now. And i added an example how we can implement it.
/*
* An Interceptor Example for __call() method
* Ersin Güvenç.
*/
Class NameWriter
{
/*
* @param - 1: name - instance of Name Class
*/
function writeName(Name $name)
{
print $name->getName();
}
function writeAge(Name $name)
{
print $name->getAge();
}
} //end of the class.
Class Name
{
private $writer; //NameWriter instance
function __construct(NameWriter $writer)
{
$this->writer = $writer;
}
/* our __call method */
function __call($methodname, $args)
{
if(method_exists($this->writer, $methodname))
{
return $this->writer->$methodname($this);
}
}
function getName() { return "John"; }
function getAge() { return "27";}
} //end of the class.
//Our Super Object.
$name = new Name( new NameWriter() );
$name->writeName();
$name->writeAge();
//OUTPUT
//Jonn
//27
When the client makes this call to Name
$name = new Name( new NameWriter() ); $name->writeName();
the call method is invoked.We find a method called writeName() in our NameWriter object and invoke it.This saves us from manually invoking the delegated method like this:
function writeName {
$this->writeName( $this );
}
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.