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
우리는 사람들에게 어떤 것도 가르칠 수 없다. 우리는 다만 그들이 자기 안에서 무언가를 찾도록 도울 수 있을 뿐이다. (갈릴레오)