我正在学习使用php autuoloader..。
据我所知,我们可以使用__autoloader或spl_autoloader_*自动加载文件。
假设这是我的目录结构:
ROOT
|
|
ADMIN
| |
| |
| DIST
| |
| SOME_FOLDER
| SOME_FOLDER
| TPL
| |
| |
| SOME_FOLDER1
| |
| test.php
| SOME_FOLDER2
| |
| example1.php
| example2.php
| example3.php
|
|
CLASSES
|
basics.php
class1.php
class2.php
class3.php
class4.php
|
index.php我在CLASSES目录中为autoload文件创建了这个类:
basics.php :
class MyAutoLoader
{
public function __construct()
{
spl_autoload_register( array($this, 'load') );
}
function load( $file )
{
//spl_autoload( 'load1' );
echo 'Try to call ' . $file . '.php inside ' . __METHOD__ . '<br>';
require( $file . '.php' );
}
}在index.php中,我将包括basics.php,每件事情都很好,因为文件存储在CLASSES文件夹中.
require_once("CLASSES/basics.php");
$loaderObject = new MyAutoLoader();使用这段代码,我可以声明class1 ... class3
现在我想要一个自动加载程序,它可以是SOME_FOLDER2中的加载文件,在本例中是example1.php、example2.php和example3.php。
我尝试了一些案例,但SOME_FOLDER2中的文件不会使用自动加载器加载。
My的尝试:
我在load2类中创建了一个名为MyAutoLoader的函数,试图包含来自SOME_FOLDER2的文件。
function load2( $file )
{
//spl_autoload_register('load2');
echo 'Inside LOADER2 ' . __METHOD__ . '<br>';
require ( 'ADMIN/TPL/' . $file . '.php' );
}我在spl_autoload_register构造函数中更改了MyAutoLoader:
$allMethods = get_class_methods( 'MyAutoLoader' );
$allMethods = array_splice( $allMethods, 1 );
foreach( $allMethods as $method )
{
spl_autoload_register( array($this, $method) );
}但他们都不适合我..。
你能告诉我我的代码出了什么问题吗?或者我对自动加载器有什么误解?
Thanks in Advance
发布于 2015-05-24 07:28:03
我认为您的问题基本上归结为在require之前不检查文件是否存在.如果文件不存在于您试图包含的第一个文件夹中,则会产生致命错误。
我不知道您的用例,但以下是一些建议:
你能只用一台自行火炮吗?
function my_autoload($class_name) {
if (is_file('CLASSES/' . $class_name . '.php')) {
require_once 'CLASSES/' . $class_name . '.php';
} else if (is_file('ADMIN/TPL/SOME_FOLDER2/' . $class_name . '.php')) {
require_once 'ADMIN/TPL/SOME_FOLDER2/' . $class_name . '.php';
}
}
spl_autoload_register("my_autoload");或者你是否需要独立地声明它们。(两个或两个以上的自动取款机):
function classes_autoload($class_name) {
if (is_file('CLASSES/' . $class_name . '.php')) {
require_once 'CLASSES/' . $class_name . '.php';
}
}
spl_autoload_register("classes_autoload");
function admin_autoload($class_name) {
if (is_file('ADMIN/TPL/SOME_FOLDER2/' . $class_name . '.php')) {
require_once 'ADMIN/TPL/SOME_FOLDER2/' . $class_name . '.php';
}
}
spl_autoload_register("admin_autoload");或者使自动加载器类成为通用类:
class MyAutoLoader {
private $path;
public function __construct($path) {
$this->path = $path;
spl_autoload_register( array($this, 'load') );
}
function load( $file ) {
if (is_file($this->path . '/' . $file . '.php')) {
require_once( $this->path . '/' . $file . '.php' );
}
}
}
$autoloader_classes = new MyAutoLoader('CLASSES');
$autoloader_admin = new MyAutoLoader('ADMIN/TPL/SOME_FOLDER2');如果您不想手动保存ADMIN/TPL中的子文件夹列表,您甚至可以从其中的任何一个中执行类似的自动加载操作(这显然假设ADMIN/TPL的所有子文件夹都包含类):
function my_autoload($class_name) {
if (is_file('CLASSES/' . $class_name . '.php')) {
require_once 'CLASSES/' . $class_name . '.php';
} else {
$matching_files = glob('ADMIN/TPL/*/' . $class_name . '.php');
if (count($matching_files) === 1) {
require_once $matching_files[0];
} else if (count($matching_files) === 0) {
trigger_error('Could not find class ' . $class_name . '!', E_USER_ERROR);
} else {
trigger_error('More than one possible match found for class ' . $class_name . '!', E_USER_ERROR);
}
}
}
spl_autoload_register("my_autoload");发布于 2015-05-24 08:23:11
实际上,您的问题可能很简单:
您的basics.php位于CLASSES文件夹中,所以当您从basics.php中获取include或require文件时,它将在该文件夹中启动。
因此,如果您想包含来自ADMIN/TPL/SOME_FOLDER2的文件,实际上需要首先“跳升”一个级别,即require '../ADMIN/TPL/SOME_FOLDER2/' . $file . '.php';。
为了避免这种混淆,我建议在index.php的开头添加一个常量,如下所示:
define('BASEPATH', __DIR__);然后,...and将其放在所有require和include语句的前面,即:
class MyAutoLoader {
public function __construct() {
spl_autoload_register( array($this, 'load') );
spl_autoload_register( array($this, 'load2') );
}
function load( $file ) {
echo 'Try to call ' . $file . '.php inside ' . __METHOD__ . '<br>';
$classfile = BASEPATH . '/CLASSES/' . $file . '.php';
if ( is_file( $classfile ) ) {
require( $classfile );
}
}
function load2( $file ) {
echo 'Try to call ' . $file . '.php inside ' . __METHOD__ . '<br>';
$classfile = BASEPATH . '/ADMIN/TPL/SOME_FOLDER2/' . $file . '.php';
if ( is_file( $classfile ) ) {
require( $classfile );
}
}
}https://stackoverflow.com/questions/30420629
复制相似问题