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
손톱만한 냉이꽃이 함박꽃이 크다고 하여 기죽어 피지 않는 일이 있는가. 사람이 각기 품성대로 능력을 키우며 사는 것, 이것도 한 송이의 꽃이라고 나는 생각한다. 《스무 살 어머니》 (정채봉)