Home  >  Article  >  Backend Development  >  Overloading and automatic loading of classes_PHP tutorial

Overloading and automatic loading of classes_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:24:50752browse

PHP4 already has overloading syntax to establish mapping to external object models, just like Java and COM. PHP5 brings powerful object-oriented overloading, allowing programmers to create custom behaviors To access properties and call methods.

Overloading can be done through several special methods: __get, __set, and __call. When the Zend engine tries to access a member and does not find it, PHP These methods will be called.

In example Figure 1, __get and __set replace all accesses to the attribute variable array. If necessary, you can implement any type of filtering you want. For example, the script can disable Set the attribute value, start with a certain prefix or include a certain type of value. The

__call method illustrates how you call an undefined method. When you call an undefined method, the method name and the parameters received by the method Will be passed to the __call method, and PHP passes the value of __call back 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()<br>n");            print("Arguments: ");            print_r($args);            return(TRUE);        }    }    $o = new Overloader();    //invoke __set() 给一个不存在的属性变量赋值,激活__set()    $o->dynaProp = "Dynamic Content";    //invoke __get() 激活__get()    print($o->dynaProp . "<br>n");    //invoke __call() 激活__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, which can include a file. After all, you know which class to use. However, PHP Provides an automatic loading function for classes, which can save programming time. When you try to use a class that PHP has not organized, it will look for a global function __autoload. If this function exists, PHP will call it with a parameter , the parameter is the name of the class.

Example Figure 2 illustrates how __autoload is used. It assumes that each file in the current directory corresponds to a class. When the script attempts to generate an instance of the class User, PHP will Execute __autoload. The script assumes that the User class is defined in class_User.php. Regardless of whether the call is in uppercase or lowercase, PHP will return the lowercase name.

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(); ?> 


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446720.htmlTechArticlePHP4 already has overloaded syntax to establish mapping to external object models, just like Java and COM . PHP5 brings powerful object-oriented overloading, allowing programmers to create custom behaviors to...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn