1
나는 내 수업에서 네임 스페이스를 사용하는 자동로드하려는하지만 난 index.php를네임 스페이스 및 자동로드 오류, 자동 네임 스페이스가있는 PHP 네임 스페이스를 올바르게 사용하려면 어떻게해야합니까?
Error: Fatal error: Class 'foo\B' not found ...
예에서 오류가 발생합니다 :
디렉토리 골격 :
\var\www
|_ foo
| |_ A.php
| |_ B.php
|
|_ view
| |_ index.php
해서, A.php
<?php
namespace foo;
class A {
private $a;
public function __construct($a) {
$this->a = $a;
}
}
2.php
<?php
namespace foo;
use foo\A;
class B extends A {
private $b;
public function __construct($a, $b) {
parent::__construct($a);
$this->b = $b;
}
}
그리고 Index.php는
<?php
use foo\B;
define('ROOT', __DIR__ . DIRECTORY_SEPARATOR);
$b = new B('s', 2);
function __autoload($classname) {
$namespace = substr($classname, 0, strrpos($classname, '\\'));
$namespace = str_replace('\\', DIRECTORY_SEPARATOR, $classname);
$classPath = ROOT . str_replace('\\', '/', $namespace) . '.php';
if(is_readable($classPath)) {
require_once $classPath;
}
}
이 질문은 이것과 거의 동일합니다 PHP autoload namespace하지만 난 다른 폴더에있는 index.php를 포함하고 작동하지 않습니다.
같은 프로젝트를 넣었지만 다음 디렉토리 스켈레톤이 있으면 아무런 오류가 발생하지 않습니다.
\var\www
|_ foo
| |_ A.php
| |_ B.php
|
|_ index.php
질문 : 나는 폴더에 index.php를 넣으면 작동하지 않는 이유는 무엇입니까?
감사
루트 상수/var/www/view/
하지
/var/www/
를 가리키고 있기 때문이다