gpt4 book ai didi

PHP 自动加载器类从两个或三个不同的路径加载文件

转载 作者:行者123 更新时间:2023-12-03 20:41:26 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Best Way To Autoload Classes In PHP

(8 个回答)


去年关闭。




我正在学习使用 php autuoloader ...

据我了解,我们可以使用 __autoloaderspl_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 中为自动加载文件创建了这个类目录 :

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.phpexample3.php
我尝试了一些情况,但 SOME_FOLDER2 中的文件不会使用自动加载器加载。

我的尝试:

我创建了一个名为 load2 的函数在 MyAutoLoader试图包含来自 SOME_FOLDER2 的文件的类
function load2( $file )
{
//spl_autoload_register('load2');

echo 'Inside LOADER2 ' . __METHOD__ . '<br>';

require ( 'ADMIN/TPL/' . $file . '.php' );

}

我改变了 spl_autoload_registerMyAutoLoader构造函数:
$allMethods = get_class_methods( 'MyAutoLoader' );
$allMethods = array_splice( $allMethods, 1 );

foreach( $allMethods as $method )
{

spl_autoload_register( array($this, $method) );

}

但没有一个对我不起作用......

你能告诉我我的代码有什么问题或者我对 auloader 有什么误解吗?

Thanks in Advance

最佳答案

我认为您的问题基本上归结为在 require 之前不检查文件是否存在ing 它...如果您尝试从中包含的第一个文件夹中不存在该文件,则会产生 fatal error 。

我不知道你的用例,但这里有一些建议:

你可以只使用一个自动加载器吗?

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");

关于PHP 自动加载器类从两个或三个不同的路径加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30420629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com