Section 11th -- overload -- ClassesandObjectsinPHP5 [11]. | Read CorePHPProgramming for Haohappy | note in ClassesandObjects chapter | main Translation + personal experience | do not repost it to avoid unnecessary troubles, thank you | = read this article for Haohappy < >
| = Notes in the Classes and Objects chapter
| = Translation-oriented + personal experiences
| = Do not repost it to avoid unnecessary troubles. thank you.
| = Thank you for your criticism and hope to make progress together with all PHP fans!
+ ------------------------------------------------------------------------------- +
*/
Section 11th -- overload
PHP 4 already has an overloaded syntax to map external object models, just like Java and COM. PHP5 brings powerful object-oriented overloading, allowing programmers to establish custom behaviors to access attributes and call methods.
You can use several special methods of _ get, _ set, and _ call for overloading. PHP will call these methods when the Zend Engine tries to access a member and cannot find it.
In Example 6.14, __get and _ set replace all accesses to the attribute variable array. if necessary, you can implement any type of filter you want. for example, a script can disable attribute values and use a certain prefix or contain certain types of values at the beginning.
The _ call method describes how to call an undefined method. when you call an undefined method, the method name and parameters received by the method will be passed to the _ call method, and the value of _ call passed by PHP will be returned to the undefined method.
Listing 6.14 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 attribute variable and 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/631483.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631483.htmlTechArticle | = this article for Haohappy read Core PHP Programming | = in the Classes and Objects chapter of the notes | = Translation + personal experience | = to avoid possible unnecessary trouble do not reprint, thank you...