AD

PHP automatically load the class

We know that in PHP5 methods can be achieved by defining __autoload loaded automatically.

For example:

function __autoload($class_name) {
require_once $class_name . '.php';
}


However, only one large-scale projects in loader function looked very powerless. Then consider ways to use dynamic loading spl.

A loader class (with reference to the realization of ZF thinking, feeling good)

 <?
 /**
 * Loader Class  
 * @author yuyii
 */
 class Loader {

         /**
          This function is used to load the file for the class, support manually invoke the function  , Manually call-time arguments dir can define path  
         @param string $class  The name of the class  
         @param string $dir (optional)  The directory name  
         */
         public static function loadClass($class,$dir = null) {
                 if (class_exists($class, false) || interface_exists($class, false)) {
                         return;
                 }
          
         // Gets the file name, class name here to go to the camel  '_' Split filenames  
         //myClass => my_class.php
         $file = strtolower(preg_replace("/([a-z0-])(?=[A-Z])/","$_",$class)).'.php';

                 if (!empty($dir) && is_string($dir)) {
                         $dir = rtrim($dir, '\\/');
                         @include_once($dir . DIRECTORY_SEPARATOR . $file);
                 } else {
                         @include_once $file;
                 }
         }

         /**
          Registration is used to automatically load function  
         @param string $class
         */
         public static function autoload($class) {
                 self::loadClass($class);
         }

         /**
          Register the add-in function of the method, you need to manually call a  , This function can also be used to load the custom loader class  , Load the classes in the autoload function  
         @param string $class (optional) The class name, the default is  Loader, The current class  
         @param bool $enabled (optional)true As a registered, false to uninstall  
         @return bool
         */
         public static function registerAutoload($class = 'Loader', $enabled = true) {
                 if(!class_exists($class)) return false;
                 if(!function_exists('spl_autoload_register')) return false;
                 $methods = get_class_methods($class);
                 if (!in_array('autoload', (array) $methods)) {
                         return false;
                 }
          
                 if ($enabled === true) {
                         return spl_autoload_register(array($class, 'autoload'));
                 } else {
                         return spl_autoload_unregister(array($class, 'autoload'));
                 }
         }

         /**
          Since loading functions  
         @param string $function
         @param bool $enabled (optional)
         @return bool
         */
         public static function registerFunction($function = null,$enabled = true) {
                 if (empty($function) || !function_exists($function)) return false;
                 if(!function_exists('spl_autoload_register')) return false;
                 if ($enabled === true) {
                         return spl_autoload_register($function);
                 } else {
                         return spl_autoload_unregister($function);
                 }
         }
 }


Code calls a method is static, in the framework of the initial position by:
Loader:: registerFunction ()
Register here loadClass, I wrote a hump in the search of papers specific to their own change

Code Features:
1. The basic function is the realization of loader
Loader:: registerFunction ()

2. Can customize the new loader (classes and functions to support the way in which class method is called autoload function)
Loader:: registerFunction ('myfunc')

3. Can manually load the class loading path can be defined at this time
Loader:: loadClass ('myClass', 'mypath')
标签: lt, amp, string class, dynamic loading, true return, interface, myclass, string function, z0, realization, array class, scale projects, bool, autoload
分类: PHP
时间: 2010-06-22

相关文章

  1. Web application automatically load ApplicationContext

    Web application automatically load ApplicationContext For Web applications, not in the code manually instantia ...
  2. Automatically load the template file to vim

    First, download vim from the following plug-ins automatically load the template file: http://www.vim.org/scrip ...
  3. tomcat class files automatically load change (without restarting tomcat)

    Tomcat does not restart in two ways: hot deployment, hot load hot deployment: the container situation in the r ...
  4. Automatically load class custom class

    <?php require_once 'exception.php'; /** * Automatically load the class * */ final class AutoloadClass { /** ...
  5. How to boot Linux automatically load the kernel module

    To figure out how to load the module automatically at boot time, the search for a long time, there are a lot o ...
  6. How to boot Linux automatically load the module

    The following is written before the time to learn Linux, and later rc.sysinit, after careful study found that ...
  7. whether to automatically load the linux partition

    We can change the configuration so that he does not own or automatically loaded automatically win partition, e ...
  8. Java Axis-1.4 calls. Net webserice (manually download the certificate and automatically load the certificate))

    Java calls. Net release of Web service can be a large number of java lib to achieve This paper uses Axis-1.4 t ...
  9. PHP automatically load the class-__autoload () and spl_autoload_register ()

    General files containing the situation less manual contains the class file to use when you want to include a l ...
  10. Debugging class files modified, automatically load the deployment of java class

    Must have all of the project development, debugging class files modified, automatically re-load the container ...
  11. Use jQuery to automatically load the iframe

    Is mainly used inside jQuery $ ('# XXX'). Attr ("src", "urlstr"); method to assign the ope ...
  12. Master a tight schedule, how to change the following code, you can automatically load the right one to open the page content?!!

    <! DOCTYPE html PUBLIC "- / / W3C / / DTD XHTML 1.0 Transitional / / EN" "http://www.w3.org/ ...
  13. ubuntu automatically load the NTFS partition

    sudo vim / etc / fstab file, add the following lines of information you can. / Dev/sda1 / media / Acer ntfs de ...
  14. Lazy Load Plugin for jQuery load the image the effect of delay

    QQ space Qiuyue Peng PPT in its reference to "demand loading" WEB performance optimization approach, ...
  15. flashfxp and mandate regular ftp program automatically backup

    The past few days has been a friend to see laggards say room above problems ah, virtual host problem or someth ...
  16. Our company's LVS + keepalived load balancing

    This is my the company's LVS + keepalived deploy load balancing solution, but some did not add the following s ...
  17. Login automatically jump across the number of seconds

    <body onload="loginTrun()"> automatically load function: <script type="text/javascript ...
  18. Automatically loaded in the Zend Framework (Learning Zend Framework Translation 2)

    Directory Describes the design goals and basic use of resources, automatically load the automatic loading conc ...
  19. (Switch) on mod_proxy + Apache 2.2.16 + Tomcat 7 load balancing and clustering configuration

    Sunday night and the GF of the villagers drank Wuliangye, leading to weeks with the get out of bed, had to lea ...