PHP spl_autoload_register()不';无法打开具有命名空间的文件
伙计们,我在使用PHP spl_autoload_register()不';无法打开具有命名空间的文件,php,Php,伙计们,我在使用spl\u autoload\u register功能时遇到了一个问题。我使用的是XAMPP在它的htdocs目录中,还有一个名为boot的目录。这个目录有两个文件,一个是carclass文件,一个是Mainphp文件。该类使用名称空间启动。我想使用这个函数spl\u autoload\u register加载那个类,但错误如下。我做错了什么 警告:require(boot\Car.php):无法打开流:C:\xampp\htdocs\boot\Main.php中没有这样的文件或
spl\u autoload\u register
功能时遇到了一个问题。我使用的是XAMPP
在它的htdocs
目录中,还有一个名为boot
的目录。这个目录有两个文件,一个是carclass
文件,一个是Main
php文件。该类使用名称空间启动。我想使用这个函数spl\u autoload\u register
加载那个类,但错误如下。我做错了什么
警告:require(boot\Car.php):无法打开流:C:\xampp\htdocs\boot\Main.php中没有这样的文件或目录
代码Car.php
<?php
namespace boot;
class Car
{
public function __construct()
{
echo 'Constructor has been created!';
}
}
几个例子:
目录结构:
project folder
- Main.php
- Car.php
- Test.php
- Foo
- - Bar.php
- - Baz
- - - Qux.php
Test.php
class Test {}
Foo\Bar.php
namespace boot\Foo;
class Bar {}
Foo\Baz\Qux.php
namespace Foo\Baz;
class Qux {}
Main.php
//__DIR__ === C:\xampp\htdocs\boot
spl_autoload_register(function ($className){
//__DIR__ . DIRECTORY_SEPARATOR .
require_once preg_replace('/^[\\\\\/]?boot[\\\\\/]?/', '', $className). '.php';
});
// both require same file but namespace must be same as defined in file
$t = new boot\Car; // work
$t = new Car; // do not work because in Car.php is namespace `boot`
// required file: C:\xampp\htdocs\boot\Car.php
// both require same file but namespace must be same as defined in file
$t = new boot\Test; // do not work because in Test.php is no namespace
$t = new Test; //work
// required file: C:\xampp\htdocs\boot\Test.php
// both require same file but namespace must be same as defined in file
$t = new boot\Foo\Bar; // work
$t = new Foo\Bar; // do not work because in Bar.php is namespace `boot\Foo`
// required file: C:\xampp\htdocs\boot\Foo\Bar.php
// both require same file but namespace must be same as defined in file
$t = new boot\Foo\Baz\Qux; // do not work because in Qux.php is namespace `Foo\Baz`
$t = new Foo\Baz\Qux; // work
// required file: C:\xampp\htdocs\boot\Foo\Baz\Qux.php
几个例子:
目录结构:
project folder
- Main.php
- Car.php
- Test.php
- Foo
- - Bar.php
- - Baz
- - - Qux.php
Test.php
class Test {}
Foo\Bar.php
namespace boot\Foo;
class Bar {}
Foo\Baz\Qux.php
namespace Foo\Baz;
class Qux {}
Main.php
//__DIR__ === C:\xampp\htdocs\boot
spl_autoload_register(function ($className){
//__DIR__ . DIRECTORY_SEPARATOR .
require_once preg_replace('/^[\\\\\/]?boot[\\\\\/]?/', '', $className). '.php';
});
// both require same file but namespace must be same as defined in file
$t = new boot\Car; // work
$t = new Car; // do not work because in Car.php is namespace `boot`
// required file: C:\xampp\htdocs\boot\Car.php
// both require same file but namespace must be same as defined in file
$t = new boot\Test; // do not work because in Test.php is no namespace
$t = new Test; //work
// required file: C:\xampp\htdocs\boot\Test.php
// both require same file but namespace must be same as defined in file
$t = new boot\Foo\Bar; // work
$t = new Foo\Bar; // do not work because in Bar.php is namespace `boot\Foo`
// required file: C:\xampp\htdocs\boot\Foo\Bar.php
// both require same file but namespace must be same as defined in file
$t = new boot\Foo\Baz\Qux; // do not work because in Qux.php is namespace `Foo\Baz`
$t = new Foo\Baz\Qux; // work
// required file: C:\xampp\htdocs\boot\Foo\Baz\Qux.php
你能确认C:\xampp\htdocs\boot\Main.php
是确切的路径吗?可能是@Thamaraiselvam的重复yes@Thamaraiselvam你重复的带标记的问题解决不了我的问题你检查过这个答案中的所有案例了吗?你能确认C:\xampp\htdocs\boot\Main.php
是确切的路径吗?可能是@Thamaraiselvam的重复yes@Thamaraiselvam你重复的带标记的问题解决不了我的问题你检查过这个答案中的所有案例了吗?