__set()
및 __get()
마법 메서드를 사용하는 간단한 클래스의 두 가지 예제가 있습니다. 하나는 치명적인 오류를 던지고 다른 하나는 unset()
기능으로 보호 된 속성에 액세스하려고 시도하지 않습니다. 동적으로 속성 이름을 만들 때 PHP 마법 메서드가 다르게 동작합니다
__set()
및
__get()
방법에 밑줄을 붙이는 명명하고있다. (밑줄없이 속성을 효과적으로 노출).
예 2에서 나는 밑줄로 이름을 시작하고 __set()
및 __get()
방법에 직접 이름을 통해 액세스 할 수 있도록 하지입니다.
질문
1) 왜 예 1 예 2 이 치명적인 오류가 발생 않지만 치명적인 오류가 발생하지 않는 이유는 무엇입니까? 나는 둘 다 오류를 던지거나 둘 다 오류를 던질 것으로 기대합니다.
2) 또한, 예제 1은 이 아니며은 실제로 속성을 설정 해제합니까? 난 unset()
함수가 호출 된 후 속성 값을 포함하지 기대하고있다.
예 1
class Example {
protected $_my_property;
function __get($name) {
echo '<h4>__get() was triggered!</h4>';
$name = '_' . $name;
if (property_exists($this, $name)) {
return $this->$name;
}
else {
trigger_error("Undefined property in __get(): $name");
return NULL;
}
}
function __set($name, $value) {
echo '<h4>__set() was triggered!</h4>';
$name = '_' . $name;
if (property_exists($this, $name)) {
$this->$name = $value;
return;
}
else {
trigger_error("Undefined property in __set(): {$name}");
}
}
}
$myExample = new Example();
$myExample->my_property = 'my_property now has a value';
echo $myExample->my_property;
unset($myExample->my_property);
echo "Did I unset my property?: {$myExample->my_property}";
예 2
보조 노트로class Example {
protected $my_property;
function __get($name) {
echo '<h4>__get() was triggered!</h4>';
if (property_exists($this, $name)) {
return $this->$name;
}
else {
trigger_error("Undefined property in __get(): $name");
return NULL;
}
}
function __set($name, $value) {
echo '<h4>__set() was triggered!</h4>';
if (property_exists($this, $name)) {
$this->$name = $value;
return;
}
else {
trigger_error("Undefined property in __set(): {$name}");
}
}
}
$myExample = new Example();
$myExample->my_property = 'my_property now has a value';
echo $myExample->my_property;
unset($myExample->my_property);
echo "Did I unset my property?: {$myExample->my_property}";
, 이것은 내가 내 실제 프로젝트에서보고 있어요 동작을 보여줍니다 그냥 단순한 예입니다 . 감사!
속성 선언을 포함하면 질문이 더 명확해질 것이라고 생각합니다. 이 동작은 속성 중 하나를 초기화하지 않아서 쉽게 발생할 수 있습니다. –
@SamDufel 고마워요,하지만 당신이 의미하는 바를 이해하지 못했습니다. 내 코드 샘플에서는 클래스 내부의 보호 된 속성을 'protected $ _my_property;'로 선언하고이를 클래스 외부에서 '$ myExample-> my_property ='my_property에 값 ';'이있는 것으로 초기화했습니다. –
죄송합니다, 분명히 나는 아침에 눈이 멀다. –