How to design a PHP auto-load class

Source: Internet
Author: User
Tags autoloader zend framework
┡┈▓libralies
┊┡┈▓database
┊┊┡┈▓driver
┊┊┊┡┈▒mysql.php
┊┊┊┡┈▒mysqli.php
┊┊┊┗┈▒sqlite.php
┊┊┡┈▓query
┊┊┊┡┈▒mysql.php
┊┊┊┡┈▒mysqli.php
┊┊┊┗┈▒sqlite.php
┊┊┡┈▒driver.php
┊┊┗┈▒query.php
┊┟┈▓session
┊┊┟┈▓storage
┊┊┊┟┈▒database.php
┊┊┊┟┈▒mamcache.php
┊┊┊┗┈▒none.php
┊┊┗┈▒storage.php
┊┡┈▒database.php
┊┡┈▒session.php

Such a file structure, how to design an automatic loading class, in the instantiation of the class when the need to load the phase of the class

Reply content:

┡┈▓libralies
┊┡┈▓database
┊┊┡┈▓driver
┊┊┊┡┈▒mysql.php
┊┊┊┡┈▒mysqli.php
┊┊┊┗┈▒sqlite.php
┊┊┡┈▓query
┊┊┊┡┈▒mysql.php
┊┊┊┡┈▒mysqli.php
┊┊┊┗┈▒sqlite.php
┊┊┡┈▒driver.php
┊┊┗┈▒query.php
┊┟┈▓session
┊┊┟┈▓storage
┊┊┊┟┈▒database.php
┊┊┊┟┈▒mamcache.php
┊┊┊┗┈▒none.php
┊┊┗┈▒storage.php
┊┡┈▒database.php
┊┡┈▒session.php

Such a file structure, how to design an automatic loading class, in the instantiation of the class when the need to load the phase of the class

It is recommended that you look at the design of the Zend Framework and the PSR standard.

The design of Autoloader can refer to the Composer of Autoloader or Autoloader of ZF1 and ZF2.

Method 1 (Simple rough)

Add a directory of classes you want to automatically load into the auto-load directory
Refer here

set_include_path('libralies/database/driver' . PATH_SEPARATOR . get_include_path()); ...

Method 2 (recommended)

Automatically loaded according to the directory or namespace
Refer here

function __autoload(){    $dir = './libralies';    set_include_path(get_include_path(). PATH_SEPARATOR. $dir);    $class = str_replace('\\', '/', $class) . '.php';     require_once($class);}

This method requires you to create a class by using the namespace method
Such as:

$a = new Libralies\Databases\Driver\Mysql;

__autoloadThe function converts it into a relative path and then imports

spl_autoload //php的框架自动加载基本上都是通过这个实现的。

Auto-load + namespace, which is the way in which almost all open source frameworks are used.

This is my approach in a project:

function core_autoload($class_name) {    $prefix = substr($class_name,0,2);    switch($prefix){        case 'm_':            $file_name = ROOT_PATH . '/app/models/' . substr($class_name, 2) . '.php';        break;        case 'a_':            $file_name = ROOT_PATH . '/app/actions/' . substr($class_name, 2) . '.php';        break;        case 'u_':            $file_name = ROOT_PATH . '/app/lib/usr/' . substr($class_name, 2) . '.php';        break;        default:            $file_name =  get_include_path() . str_replace('_', '/', $class_name).'.php';    }    if( file_exists($file_name) )            require_once $file_name;    else spl_autoload($class_name);}spl_autoload_register('core_autoload');

Recommended namespaces
can be used PSR-0 standard http://www.php-fig.org/psr/psr-0/

Build justice to see the realization of composer

Composer can be achieved, why do they have to develop their own?

The younger brother uses the same file name and the class name loading way! Wang Consultation fee

Use the Spl_autoload_register function to register the class load function, where the class name is separated by the path name by _

Can see the Symfony automatic class loading, super praise. Back and then see thinkphp

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.