고생하십니다. 새해 복 많이 받으세요 선생님.
코드는 다음과 같습니다.
<?php
/**
* Classes Autoloading (PSR-4)
*/
// include './Classes/MyClass.php';
use Classes\MyClass;
spl_autoload_register(function ($classname) {
include $classname . '.php';
});
new MyClass();
디렉토리 구조는
/Users/chamusong/Desktop/php-oop/functions/SPL/4
Classes-MyClass.php index.php
이렇게 되어있습니다.
오류내용은 다음과 같습니다.
PHP Warning: include(Classes\MyClass.php): failed to open stream: No such file or directory in /Users/chamusong/Desktop/php-oop/functions/SPL/4/index.php on line 13
PHP Stack trace:
PHP 1. {main}() /Users/chamusong/Desktop/php-oop/functions/SPL/4/index.php:0
PHP 2. spl_autoload_call($class_name = 'Classes\\MyClass') /Users/chamusong/Desktop/php-oop/functions/SPL/4/index.php:16
PHP 3. {closure:/Users/chamusong/Desktop/php-oop/functions/SPL/4/index.php:12-14}($classname = 'Classes\\MyClass') /Users/chamusong/Desktop/php-oop/functions/SPL/4/index.php:16
PHP Warning: include(): Failed opening 'Classes\MyClass.php' for inclusion (include_path='.:/opt/homebrew/Cellar/php@7.4/7.4.27/share/php@7.4/pear') in /Users/chamusong/Desktop/php-oop/functions/SPL/4/index.php on line 13
PHP Stack trace:
PHP 1. {main}() /Users/chamusong/Desktop/php-oop/functions/SPL/4/index.php:0
PHP 2. spl_autoload_call($class_name = 'Classes\\MyClass') /Users/chamusong/Desktop/php-oop/functions/SPL/4/index.php:16
PHP 3. {closure:/Users/chamusong/Desktop/php-oop/functions/SPL/4/index.php:12-14}($classname = 'Classes\\MyClass') /Users/chamusong/Desktop/php-oop/functions/SPL/4/index.php:16
PHP Fatal error: Uncaught Error: Class 'Classes\MyClass' not found in /Users/chamusong/Desktop/php-oop/functions/SPL/4/index.php:16
Stack trace:
#0 {main}
thrown in /Users/chamusong/Desktop/php-oop/functions/SPL/4/index.php on line 16
오류내용을 보면 경로의 문제인 것 같기도 합니다.