Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP spl_autoload_register()不';无法打开具有命名空间的文件_Php - Fatal编程技术网

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你重复的带标记的问题解决不了我的问题你检查过这个答案中的所有案例了吗?