How to design a php automatic loading class

Source: Internet
Author: User
Tags autoloader
There are too many databases under libralies. php has been installed successfully without using mysqli. php has been installed successfully using sqlite. php has been running too many queries when there are too many other mysql instances. php has been installed successfully without using mysqli. php has been installed successfully using sqlite. php plugin driver .... registry.libralies
Too many databases
Zookeeper has zookeeper driver
Mysql. php
Too many concurrent connections using mysqli. php
Please refer to the following link for more information: sqlite. php.
Too many queries
Mysql. php
Too many concurrent connections using mysqli. php
Please refer to the following link for more information: sqlite. php.
Too many other drivers driver. php
Too many queries. php
Too many concurrent sessions
When there are too many slave storage
Too many rows have been written into database. php
Too many concurrent connections using mamcache. php
Please refer to the following link for more information: none. php
Too many memory exceeds storage. php
Too many other databases. php
Too many concurrent sessions. php

How to design an automatic loading class for such a file structure and load corresponding classes as needed during class instantiation

Reply content:

Registry.libralies
Too many databases
Zookeeper has zookeeper driver
Mysql. php
Too many concurrent connections using mysqli. php
Please refer to the following link for more information: sqlite. php.
Too many queries
Mysql. php
Too many concurrent connections using mysqli. php
Please refer to the following link for more information: sqlite. php.
Too many other drivers driver. php
Too many queries. php
Too many concurrent sessions
When there are too many slave storage
Too many rows have been written into database. php
Too many concurrent connections using mamcache. php
Please refer to the following link for more information: none. php
Too many memory exceeds storage. php
Too many other databases. php
Too many concurrent sessions. php

How to design an automatic loading class for such a file structure and load corresponding classes as needed during class instantiation

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

For Autoloader design, see ZF1 and ZF2 Autoloader or Composer Autoloader.

Method 1 (simple and crude)

Add the directory of the class you want to automatically load to the automatically loaded directory.
Refer to here

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

Automatically load according to directory or namespace
Refer to 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 use the namespace method when creating a class.
For example:

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

__autoloadThe function will convert it into a relative path and then import

The spl_autoload // php framework is basically implemented through automatic loading.

Automatically load + namespace, which is used by almost all open-source frameworks.

This is my practice 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 namespace
PSR-0 standard http://www.php-fig.org/psr/psr-0/ can be used

See the implementation of composer.

Composer can be implemented. Why do I need to develop it myself?

The younger brother uses the same loading method as the class name! Wang xiaona

Use the spl_autoload_register function to register a class to load a function. The class name is separated by _ Based on the path name.

Let's take a look at automatic class loading in symfony, which is awesome. 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.