2010-06-02 3 views
0

나는 함수에서 전달 된 클래스의 문자열 이름 만 가진 함수에서 이미 초기화 된 클래스를 사용하려고합니다.클래스의 이름 만 문자열로 가진 액세스 클래스

예 : 도움을

class art{ 
    var $moduleInfo = "Hello World"; 
} 

$art = new Art; 

getModuleInfo("art"); 

function getModuleInfo($moduleName){ 
    //I know I need something to happen right here. I don't want to reinitialize the class since it has already been done. I would just like to make it accessible within this function. 

    echo $moduleName->moduleInfo; 
} 

감사합니다!

답변

1

var $moduleInfo은 $ moduleInfo를 (php4 style, public) 인스턴스 속성으로 만듭니다. 즉, 클래스 art의 각 인스턴스에는 자체 $ moduleInfo 멤버가 있습니다. 그러므로 수업의 이름이 너를 잘하지 않을 것이다. 참조 할 인스턴스를 지정하거나 전달해야합니다.
은 아마 당신은 http://docs.php.net/language.oop5.static

class art { 
    static public $moduleInfo = "Hello World"; 
} 

getModuleInfo("art"); 

function getModuleInfo($moduleName){ 
    echo $moduleName::$moduleInfo; 
} 
0

는 매개 변수로 함수에 객체 자체를 전달 참조 정적 속성 찾고 있습니다.

class art{ 
    var $moduleInfo = "Hello World"; 
} 

function getModuleInfo($moduleName){ 
    //I know I need something to happen right here. I don't want to reinitialize the class since it has already been done. I would just like to make it accessible within this function. 

    echo $moduleName->moduleInfo; 
} 

$art = new Art; 

getModuleInfo($art); 
관련 문제