PHP & Others

클래스로더 2

페이지 정보

본문

<?php
class classLoader {
    function getInstanceOf($module) {
        $file = $module.".class.php";
        require_once($file);
        return true;
        }

    function init ($module, $ar='') {
        if (classLoader::getInstanceOf($module)) {
            if($ar) {
                if(is_object($ar) ){
                    $params=&$ar;
                    eval ("\\$retval = new $module(\\$params);");
                    return $retval;
                } else if (is_string($ar)) {
                    $params = $ar;
                } else {
                    $params = array();
                       
                    for ($i=0; $i<sizeof($ar); $i++) {
                        $params[] = '"'.$ar[$i].'"';
                    }
           
                    $params = implode(',',$params);
                }
            } else {
                $params = '';
            }
            eval ("\\$retval = new $module($params);");
           
        }
        return $retval;
    }
}

$tpl->db = classLoader::init('DB_',array("localhost","xxx","xxx","xxx"));
$tpl->session = classLoader::init('SESSION_',$tpl->db);
?>

아래거친마루님께서 작성하신것과 동일한 것입니다.
상당히 비슷한데 오브젝트를 넘길수 있고 파라메터값들은 모두 배열로 넘깁니다.
array가 아니면 그냥 하나구요


ps. 신기하네요 거친마루님께 많이 배우는데 비슷한 생각을 했었다는것이...

관련자료

등록된 댓글이 없습니다.
Today's proverb
한가로움이란 아무할 일이 없어졌다는 것이 아니라 다른 새로운 일을 시작할 수있는 기회가 생긴 것이다.