Home >Backend Development >PHP Tutorial >PHP solves multiple automatic loading conflicts in the project
The automatic loading mechanism in some frameworks directly reports an error when it is found that it cannot be loaded, without transferring control to the next automatic loading method. For example, if I want to introduce the Alibaba Cloud Log Service Interface SDK, the SDK It comes with an automatic loading method, as follows:
<?php /** * Copyright (C) Alibaba Cloud Computing * All rights reserved */ $version = '0.6.0'; function Aliyun_Log_PHP_Client_Autoload($className) { $classPath = explode('_', $className); if ($classPath[0] == 'Aliyun') { if(count($classPath)>4) $classPath = array_slice($classPath, 0, 4); $filePath = dirname(__FILE__) . '/' . implode('/', $classPath) . '.php'; if (file_exists($filePath)) require_once($filePath); } } spl_autoload_register('Aliyun_Log_PHP_Client_Autoload');
The above automatic loading method will conflict with the original framework's own loading method. The solution is as follows:
<?php function autoloadAdjust() { // 取原有的加载方法 $oldFunctions = spl_autoload_functions(); // 逐个卸载 if ($oldFunctions){ foreach ($oldFunctions as $f) { spl_autoload_unregister($f); } } // 注册本框架的自动载入 spl_autoload_register( # 就是aliyun sdk的加载方法 function ($className) { $classPath = explode('_', $className); if ($classPath[0] == 'Aliyun') { if(count($classPath)>4) $classPath = array_slice($classPath, 0, 4); unset($classPath[0]); $filePath = dirname(__FILE__) . '/' . implode('/', $classPath) . '.php'; if (file_exists($filePath)) require_once($filePath); } } ); // 如果引用本框架的其它框架已经定义了__autoload,要保持其使用 if (function_exists('__autoload')) { spl_autoload_register('__autoload'); } // 再将原来的自动加载函数放回去 if ($oldFunctions){ foreach ($oldFunctions as $f) { spl_autoload_register($f); } } } # 最后调用上面方法 autoloadAdjust();
Note that when introducing it, use it according to the above method. You may need to change the file path in the code
Reference:
Recently, during development, the ZF framework and a self-owned framework are used to cooperate.
Start first After installing ZF, after starting its own framework, it was found that the automatic loading of its own framework did not take effect.
Both parties used spl_autoload_register to register the automatic loading method.
After analysis, it was found , ZF's loading method directly reports an error when it is found that it cannot be loaded, without transferring control to the next automatic loading method.
If you register the loading method of your own framework first, there will be no problem. Because when the automatic loading method of the own framework cannot find the class, it will return False, which transfers control to the next loading method
The project status causes the registration order to be ZF first. I checked the manual and wrote Use the following procedure to adjust the registration order
The above is the detailed content of PHP solves multiple automatic loading conflicts in the project. For more information, please follow other related articles on the PHP Chinese website!