그래, 문제가 생겼어. 미안해. 분명히 설명 할 수는 없지만 코드는 스스로를 말해 준다.인스턴스화 된 객체에서 매직 메소드 __set()
주어진 클래스 이름에서 개체를 생성하는 클래스가 있습니다.
public function name($name)
{
$this->includeModule($name);
try
{
$module = new ReflectionClass($name);
$instance = $module->isInstantiable() ? $module->newInstance() : "Err";
$this->addDelegate($instance);
}
catch(Exception $e)
{
Modules::Name("Logger")->log($e->getMessage());
}
return $this;
}
AddDelegate 방법 : 말은 우리는 클래스 모듈라고
protected function addDelegate($delegate)
{
$this->aDelegates[] = $delegate;
}
__call 방법
public function __call($methodName, $parameters)
{
$delegated = false;
foreach ($this->aDelegates as $delegate)
{
if(class_exists(get_class($delegate)))
{
if(method_exists($delegate,$methodName))
{
$method = new ReflectionMethod(get_class($delegate), $methodName);
$function = array($delegate, $methodName);
return call_user_func_array($function, $parameters);
}
}
}
__get 방법
public function __get($property)
{
foreach($this->aDelegates as $delegate)
{
if ($delegate->$property !== false)
{
return $delegate->$property;
}
}
}
모든 이 괜찮 함수 __set
public function __set($property,$value)
{
//print_r($this->aDelegates);
foreach($this->aDelegates as $k=>$delegate)
{
//print_r($k);
//print_r($delegate);
if (property_exists($delegate, $property))
{
$delegate->$property = $value;
}
}
//$this->addDelegate($delegate);
print_r($this->aDelegates);
}
class tester
{
public function __set($name,$value)
{
self::$module->name(self::$name)->__set($name,$value);
}
}
Module::test("logger")->log("test"); // this logs, it works
echo Module::test("logger")->path; //prints /home/bla/test/ this is also correct
을 기대 작품하지만 난 클래스 로거의 경로 속성이 공개되어이
Module::tester("logger")->path ="/home/bla/test/log/";
같은 수준의 로그 값을 설정 질수 있으므로 보호 또는 개인 재산이 아니 문제 접속하다.
어떻게이 문제를 해결할 수 있습니까? 나는 나의 문제를 분명히 설명 할 수 있었으면 좋겠다.
편집 : 간단한 데모
Modules::Name("XML_Helper")->xmlVersion ="Hello"; // default is 333
$a = Modules::Name("XML_Helper")->xmlVersion; // now $a should contain "Hello"
echo $a; // prints 333
난입니다 무엇이 필요
Modules::Name("XML_Helper")->xmlVersion ="Hello"; // default is 333
$a = Modules::Name("XML_Helper")->xmlVersion; // now $a should contain "Hello"
echo $a; // prints Hello
문제를 보여주는 간단한 독립 스크립트 (예 : 복사 및 붙여 넣기 가능) 스크립트를 제공 할 수 있습니까? – VolkerK
@VolkerK 여기 – streetparade
위의 예에서 보면도 print_r의 배열 이다있다 ( [0] => XML_Helper 개체 ( [xmlVersion] => 안녕 ) ) 정확한 보이지만 어디에 오류가 ? – streetparade