class Test { public $name; } /*******/ class MyClass { private $_test = NULL; __get($name) { return $this->$name; } __set($name,$value) { $this->$name = $value; } }
Prawidłowe użycie tego to np. :
$obj1 = new MyClass(); $obj1->_test = new Test(); $obj1->_test->name = 'Test!';
Wtedy wszystko działa jak chcę. Ale jest też możliwość, że ktoś użyje tego tak:
$obj1 = new MyClass(); $obj1->_test->name = 'Test!';
W wyniku czego dostaje notice: "Notice: Indirect modification of overloaded property MyClass::$_test has no effect in /not_important_path_here". Chciałbym nie dopuścić do takiej sytuacji. Zamiast notice chciałbym wyrzucić wyjątek. Jak to zrobić?