Composer의 autoload 기능에서 발생하는 오류는 대부분 클래스를 찾지 못하거나 네임스페이스 문제가 주요 원인입니다.
문제: vendor/autoload.php 파일이 없거나 손상됨
# Composer 설치 확인
composer --version
# 의존성 설치 및 autoload 생성
composer install
# autoload 파일만 재생성
composer dump-autoload
<?php
// autoload 파일 포함 확인
$autoload_file = __DIR__ . '/vendor/autoload.php';
if (!file_exists($autoload_file)) {
die('Composer autoload 파일이 없습니다. "composer install"을 실행하세요.');
}
require_once $autoload_file;
// 클래스 사용
use App\Models\User;
use App\Services\EmailService;
try {
$user = new User();
$emailService = new EmailService();
} catch (Error $e) {
echo "클래스 로드 오류: " . $e->getMessage();
}
?>
문제: composer.json에서 네임스페이스와 디렉토리 매핑이 잘못됨
{
"name": "myproject/webapp",
"autoload": {
"psr-4": {
"App\\": "src/",
"App\\Controllers\\": "src/Controllers/",
"App\\Models\\": "src/Models/",
"App\\Services\\": "src/Services/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
}
}
<?php
namespace App\Models;
class User
{
private $id;
private $name;
public function __construct($name = null)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
?>
<?php
namespace App\Controllers;
use App\Models\User;
use App\Services\UserService;
class UserController
{
private $userService;
public function __construct()
{
$this->userService = new UserService();
}
public function createUser($name)
{
return new User($name);
}
}
?>
문제: 특정 파일이나 레거시 클래스가 autoload되지 않음
{
"autoload": {
"psr-4": {
"App\\": "src/"
},
"classmap": [
"legacy/",
"lib/OldClass.php"
],
"files": [
"src/helpers.php",
"config/constants.php"
]
}
}
<?php
// 전역 헬퍼 함수들
if (!function_exists('debug_log')) {
function debug_log($message) {
error_log('[DEBUG] ' . $message);
}
}
if (!function_exists('format_currency')) {
function format_currency($amount) {
return number_format($amount, 2) . ' 원';
}
}
?>
<?php
// 전역 상수 정의
define('APP_VERSION', '1.0.0');
define('DEFAULT_TIMEZONE', 'Asia/Seoul');
define('MAX_UPLOAD_SIZE', 10 * 1024 * 1024); // 10MB
?>
문제: 프로덕션 환경에서 autoload 성능이 느림
# 개발 환경에서 autoload 재생성
composer dump-autoload --dev
# 프로덕션 환경에서 최적화된 autoload 생성
composer dump-autoload --optimize --no-dev
# 클래스맵 최적화 (더 빠른 로딩)
composer dump-autoload --classmap-authoritative
<?php
// 프로덕션 환경 설정
if (getenv('APP_ENV') === 'production') {
// OPcache 설정 확인
if (function_exists('opcache_get_status')) {
$opcache_status = opcache_get_status();
if (!$opcache_status['opcache_enabled']) {
error_log('경고: OPcache가 비활성화되어 있습니다.');
}
}
}
// Composer autoload
require_once __DIR__ . '/../vendor/autoload.php';
// 애플리케이션 초기화
date_default_timezone_set(DEFAULT_TIMEZONE);
?>
문제: 특정 클래스가 로드되지 않는 원인을 찾기 어려움
<?php
require_once __DIR__ . '/../vendor/autoload.php';
// 등록된 autoloader 확인
$autoloaders = spl_autoload_functions();
echo "등록된 autoloader 수: " . count($autoloaders) . "\n";
// Composer autoloader 정보 확인
$loader = require __DIR__ . '/../vendor/autoload.php';
// PSR-4 네임스페이스 확인
$psr4 = $loader->getPrefixesPsr4();
echo "PSR-4 네임스페이스:\n";
foreach ($psr4 as $namespace => $paths) {
echo " $namespace => " . implode(', ', $paths) . "\n";
}
// 클래스맵 확인
$classMap = $loader->getClassMap();
echo "\n클래스맵 항목 수: " . count($classMap) . "\n";
// 특정 클래스 로드 테스트
function testClassLoad($className) {
echo "\n클래스 '$className' 테스트:\n";
if (class_exists($className)) {
echo " ✓ 로드 성공\n";
$reflection = new ReflectionClass($className);
echo " 파일: " . $reflection->getFileName() . "\n";
} else {
echo " ✗ 로드 실패\n";
// 예상 파일 경로 확인
$expectedFile = str_replace(['\\', 'App/'], ['/', 'src/'], $className) . '.php';
echo " 예상 경로: $expectedFile\n";
echo " 파일 존재: " . (file_exists($expectedFile) ? '예' : '아니오') . "\n";
}
}
// 테스트할 클래스들
testClassLoad('App\\Models\\User');
testClassLoad('App\\Controllers\\UserController');
testClassLoad('App\\Services\\EmailService');
?>
# autoload 디버그 실행
php debug/autoload_debug.php
# Composer 검증
composer validate
# 의존성 문제 진단
composer diagnose
/ 사용 권장dump-autoload 실행