Derick Rethans wrote:
Hello!
we're finding some problems with property overloading (__get() and
__set()). Here is an RFC describing what we'd like to see changed.
Please comment on this.
Introduction:
PHP currently supports property overloading with the magic functions __get()
and __set(). Those methods are called when the code "echo $object->a" or
"$object->a = 42" are executed and the property "a" is not declared in the
class. The magic methods are responsibly for:
- checking if the property actually "exists"
- setting the value or returning the value depending on the action
Problems:
1. There is no way to document the 'virtual' properties with any of the existing
documentation tools (such as phpdoc and doxygen)
2. There is no way how the magic methods know if a specific 'virtual' property
exists or not as those properties are not declared usually - unless you
define an array with property names as a class constant (which is not
allowed either).
3. There is no way for the magic methods to return a meaningfull error when a
property doesn't "exist". Of course it is possible to throw an error with
"trigger_error" or "throw" in case a property doesn't "exist" in a specific
class, but the file and line numbers would not match the actually get/set
action. debug_backtrace() can be used to retrieve the correct file and line,
but as you have to do this for every class where you want to use setters and
getters *and* you have to implement your own error message rendering
function this is not really a suitable option either.
what about:
<?php
class GetSet0 {
function __get($name) {}
function __set($name, $value) {}
}
class GetSet1 {
property $x;
// or virtual $x, ...
}
class GetSet2 {
property $x { __get, __set }
function __get($name) {}
function __set($name, $value) {}
}
class GetSet3 {
property $x { getMethod, setMethod }
function getMethod() {}
function setMethod($value]) {}
}
?>
Features:
1) Easy way to document properties
2) No BC break with code relies on __get, __set
Clases GetSet0, GetSet1 and GetSet2 are equivalent and third class
GetSet3 use different methods to handle $x property;
There is a nice way to declare reaonly properties
property $x { getMethod }
or
property $x { getMethod, null }
and writeonly propeties :)
property $x { null, setMethod }
If someone touch undeclared property and class don't have __get/__set
Zend engine can raise exception ... It's good way? I don't know.
--
Ondrej Ivanic
([EMAIL PROTECTED])
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php