Auto load of overload and class. 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. PHP4 already has an overloaded syntax to build a ing of 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.
Heavy loadYou can use several special methods such as _ get, _ set, and _ call. PHP will call these methods when the Zend Engine tries to access a member and cannot find it.
In this example, __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.
User-level overloading
<? Php 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 () activation _ call () $ o-> dynaMethod (" Leon "," Zeev ");?> |
Automatic loading of classes
When you try to use an undefined class, PHP will report a fatal error. the solution is to add a class and include a file. after all, you know which class to use. however, PHP provides the automatic class loading function, which can save programming time. when you try to use a class that is not organized by PHP, it will look for a global function of _ autoload. if this function exists, PHP will use a parameter to call it. the parameter is the name of the class.
The example illustrates how _ autoload is used. it assumes that each file in the current directory corresponds to a class. when the script tries to generate a User-like instance, PHP will execute _ autoload. the script assumes that class_User.php defines the User class .. PHP returns the lower-case name no matter whether it is in upper or lower case.
Class autoloading
<?php //define autoload function function __autoload($class) { include("class_" . ucfirst($class) . ".php"); } //use a class that must be autoloaded $u = new User; $u->name = "Leon"; $u->printName(); ?> |
Ghost. PHP5 brings powerful object-oriented overloading, allowing programmers to establish custom behaviors...