php에서 include하기 귀찮죠?

출처 : http://kr2.php.net/manual/en/language.oop5.autoload.php 하단의 user note 참고

PHP5로 Class를 잘(?) 사용해서 프로그래밍 해보려고 하다가
__autoload라는 메서드를 찾게되었다.
이미 모두 알고있었겠지만 ㅜㅜ


//filename : autoLoad.php
define("CLASS_DIR", dirname(__FILE__));

function __autoload($className) {
$folder = classFolder($className);

if( $folder )
require_once( $folder.$className.".php" );
}

function classFolder($className, $sub = "/") {
$dir = dir(CLASS_DIR.$sub);

if( file_exists(CLASS_DIR.$sub.$className.".php") )
{
return CLASS_DIR.$sub;
}

while( false !== ($folder = $dir->read()) ) {
if( $folder != "." && $folder != ".." ) {
if( is_dir(CLASS_DIR.$sub.$folder) ) {
$subFolder = classFolder( $className, $sub.$folder."/" );

if( $subFolder )
{
return $subFolder;
}
}
}
}
$dir->close();
return false;
}


사용법은 아래와 같다.




require_once( '/home/user/lib/classLoader.php' );

$uploader = new Uploader( $json_path );
$uploader->run();






/home/user/lib의 하위 디렉터리에 Uploader.php라는 것을 찾아 자동으로 require_once 해주게 되는 것이다.


php5에 대한 새로운 기능(벌써 3년 됬죠 ㅡㅡ)을 알고 싶으면 다음 URL을 참고.

http://www.zdnet.co.kr/builder/dev/web/0,39031700,39132314,00.htm
http://www.ihelpers.co.kr/programming/lec.php?CMD=view&TYPE=0&KEY=&SC=S&&CC=&PAGE=1&IDX=252



덧1) 예전 코드를 Class로 바꿔 보려니 무지 짜증나네요.

이 글과 관련있는 글을 자동검색한 결과입니다 [?]

by 스팟 | 2007/06/23 16:34 | 개발 | 트랙백 | 덧글(0)

트랙백 주소 : http://iklo.egloos.com/tb/3533563
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]

:         :

:

비공개 덧글

◀ 이전 페이지          다음 페이지 ▶