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
진실을 말하는 자가 되어라. 외톨박이가 되는 것을 겁내지 말아라. 친구들은 끝내는 돌아올 것이다. (로댕)