반응형

PHP 프로젝트를 이용할떄 Composr 를 이용해서 본인이 사용한 작성한 클래스를 불러와 사용하는 방법에 대해 서술한다. 

 

절차:

 

0. composer 설치및 composer init

1. PSR-4 표준에 맞는 폴더구조로 클래스파일과 함께 생성 (이때 네임스페이스에 주의한다)  

2. composer.json 에 폴더 연결 

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},
"autoload-dev": {
    "classmap": [
        "tests/TestCase.php",
        "tests/PassportTestCase.php"
    ],
    "psr-4": {
        "Tests\\": "tests/"
    }
},

3. composer auto-dumpload 를 하면 클래스 위치들을 맵핑해주는 파일이 자동 생성된다.

4. 클래스를 사용하고자 하는 파일에서 autoload.php 파일 로드

 

** 주의점:

1. 클래스명과 파일명이 일치

2.한파일에는 클래스 하나만 존재

 

예제 파일구조를 본다

src

autoload.php

index.php

Users

User.php

UserDetail.php

 

 

index.php 에서 사용하자하면

require("./autoload.php")

 

- User.php 

<?php namespace Users;

 

class User { }

 

- UserDetail.php 

<?php namespace Users;

 

class UserDetail { }

 

- index.php

 

<?php

use Users\User;

$user = new User();

 

 

 

 

 

 

 

반응형

'PHP > PHP 문법' 카테고리의 다른 글

설치된 PHP가 threadsafe 혹은 nonthreadsafe 인지 알아내는법  (0) 2016.07.23
DB접속 (MySQL 기준)  (0) 2016.04.21

+ Recent posts