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
좋은 책을 읽는 것은 과거의 가장 뛰어난 사람들과 대화를 나누는 것과 같다. (데카르트)