PHP4 already has overloaded syntax to build mappings to the external object model, just like Java and COM. PHP5 brings a powerful object-oriented overload that allows programmers to create custom behaviors to access properties and invoke methods.
Overloading can be done through __get, __set, and __call several special methods. PHP will call these methods when the Zend engine tries to access a member and does not find it.
In the following example, __get and __set Replace all access to an array of attribute variables. If necessary, you can implement any type of filter you want. For example, a script can prohibit setting a property value, starting with a certain prefix or containing a certain type of value.
The __call method shows you how to invoke an undefined method. When you call an undefined method, the method name and the parameter received by the method are passed to the __call method, and the value of the PHP pass __call is returned to the undefined method.
Listing1 User-level Overloading
Class Overloader
{
Private $properties = Array ();
function __get ($property _name)
{
if (Isset ($this->properties[$property _name]))
{
Return ($this->properties[$property _name]);
}
Else
{
return (NULL);
}
}
function __set ($property _name, $value)
{
$this->properties[$property _name] = $value;
}
function __call ($function _name, $args)
{
Print ("Invoking $function _name () n");
Print ("Arguments:");
Print_r ($args);
return (TRUE);
}
}
$o = new Overloader ();
Invoke __set () assigns a value to a non-existent property variable, activates __set ()
$o->dynaprop = "Dynamic Content";
Invoke __get () activate __get ()
Print ($o->dynaprop. "N");
Invoke __call () activate __call ()
$o->dynamethod ("Leon", "Zeev");
? >
http://www.bkjia.com/PHPjc/532421.html www.bkjia.com true http://www.bkjia.com/PHPjc/532421.html techarticle PHP4 already has overloaded syntax to build mappings to the external object model, just like Java and COM. PHP5 brings powerful object-oriented overloads that allow programmers to build custom behaviors to ...