autoloading error 질문입니다.
313
10 asked
고생하십니다. 새해 복 많이 받으세요 선생님.
코드는 다음과 같습니다.
<?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
오류내용을 보면 경로의 문제인 것 같기도 합니다.
Answer 2
1
안녕하세요. 경로가 문제라는 사실을 이미 알고계시기 때문에 따로 답변을 드릴 내용이 사실상 없습니다. 다시 한 번 체크해보실 필요가 있습니다. 아래의 코드는 제가 테스트해본 것입니다. 폴더구조와 MyClass.php 에 정의된 네임스페이스도 올바르게 선언되어 있는지 확인해보시기 바랍니다.
Directory Tree
Project (Directory)
└ Classes
└ MyClass.php
└ index.php
Classes/MyClass.php
namespace Classes;
class MyClass
{
}
index.php
use Classes\MyClass;
spl_autoload_register(function ($classname) {
include $classname . '.php';
});
new MyClass();
0
추측이지만 맥 환경과 윈도우 환경의 디렉토리 참조자가 달라서 그런 것 같습니다.
<?php
/**
* Classes Autoloading (PSR-4)
*/
// include './Classes/MyClass.php';
use \Classes\MyClass;
/* spl_autoload_register(function ($classname) {
var_dump($classname);
include $classname . '.php';
}); */
spl_autoload_register(function ($classname) {
include './' . str_replace('\\', '/', $classname . '.php');
});
$myc = new MyClass();
$myc->foo();
위와 같이 replace를 통해 "\" 문자를 "/"로 변경 후 include한 결과 잘 됐는데 원래 의도한 동작과 일치하는 지 궁금합니다.
패케지스트를 하지 않고 섹션7을 수행할수 있나요
0
90
1
나만의 프레임워크 작성 후, 운영환경 배포에 관한 문의 드려요
0
402
1
35강 미들웨어에서 인증 과정을 Auth클래스로 만들면, 어느 디렉토리에 둘까요?
0
335
1
ios환경에서는 어떻게 설치해야 하나요?
0
448
1
라라벨(php) 배포 문의
0
1652
1
왜 http://localhost/ 로 접근하면 public/index.php 가 실행되나요?
0
2102
1
http://localhost:8080/posts/1 접근시 배열 0값 표기 오류
0
397
1
xdebug 설치
0
668
1
localhost 경로
0
441
1
class not found
1
328
1
namespace class not found 질문
0
466
1
csrfmiddelware 에러 질문입니다
0
363
2
url Parttern Parts 가 무슨뜻인가요?
0
349
1
통합 개발환경 (IDE) 관련 문의
0
248
1
Route 클래스 질문이 있습니다!
0
297
1
모델 User 등에서 속성들을 protected 나 private 으로 설정하지 않는 이유가 있습니까?
0
340
1
안녕하세요 프로젝트를 react와 연동하기에 대해서 여쭈고 싶습니다 :)
1
1377
1
DatabaseSessionHandler.php 에러 해결 부분
0
299
1
안녕하세요. 라우터 서버 연결 질문이 있습니다.
1
281
1
아니 강사님 왜 자꾸 어어 거려요? 진짜 겁나 거슬려 죽겠네 ㅡㅡ 어좀 적당히해요 거슬려 죽겠네 그리고 마이크 좀 좋은거 쓰세요 볼륨이 낮아졌다 커졌ㄷ ㅏ하고 발음도 다뭉개져지고 딕션도 안 좋아서 하나도 안들려요 진짜 개짜증나네
1
574
2
로컬환경에서 개발 후 운영환경으로 배포 시 발생하는 이슈에 대한 문의입니다.
1
575
2
DatabaseSessionHandler 클래스의 gc($maxlifetime) 메소드에 대해 질문드립니다.
2
259
1
Xdebug 설치 후 php index.php 실행하면 오류가 납니다..!
0
1135
1
CentOS 7.* 서버쪽으로 배포를 해보던 중에 질문드립니다.
1
371
1

