2014-04-30 7 views
1

PHP에서 클래스를 자동로드하는 중에 문제가 있습니다. 에서PHP : 자동로드 클래스를 찾을 수 없습니다.

index.php 내가 (즉, simpliest 하나) 쓰기 :

function _autoload($class_name) { 
    require_once $class_name . '.php'; 
} 

$a = new Cont(); 

Cont.php 파일이 PROJECT_ROOT/assets/core/Contr.php에 있습니다; 내 index.php 파일에 치명적인 오류가 발생 결과

: 당신이 밑줄 문자를 놓친처럼

Fatal error: Class 'Cont' not found in /var/www/bill/index.php on line 15 

답변

3

그것은해야 __autoload() 보인다. 두 밑줄 -

이다

있습니다 .. PHP Manual...

spl_autoload_register() provides a more flexible alternative for autoloading classes. For this reason, using __autoload() is discouraged and may be deprecated or removed in the future.

1

처음부터

function __autoload($class_name) { 
    require_once $class_name . '.php'; 
} 

$a = new Cont(); 

팁은, 그것이 __autoload이다.

둘째, 그 기술은 권장하지 않습니다. spl_autoload_register이 더 좋은 옵션입니다.

세 번째로 파일이 PROJECT_ROOT/assets/core 인 경우 require_once 'assets/core/' . $class_name . '.php';이 필요합니다.

+0

예. 바보 같은 실수를 해줘서 고마워. 누락 된 밑줄을 발견하지 못했다. –