┡┈▓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;
__autoload
The 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