Hello Sara,
while all what you wrote is doable i'd suggest a slightly different
approach. If the name of the variable is prefixed with "r_" and written
from outside the class an error will be issued.
There's only one thing to be considered. The proposal cannot work for
overloaded internal objects like those in ext/dom for example.
Friday, May 12, 2006, 6:14:20 PM, you wrote:
>>> __get() and __set() are great, but 90% of the time, I find myself
>>> using them to create public readonly properties.
>>>
>>I can see where it could come in handy but I honestly think it'd be bloat.
>> We have to relax with the OO features because the increased code size has
>> already made it harder to maintain and it has the potential to make PHP
>> far more complicated than what it should be.
>>
> An extension could accomplish this by exporting an interface which overrides
> the object's read_property() method. One could "flag" which properties are
> allowed to be accessed r/o by giving them a distinct name e.g.:
> class foo implements ReadOnlyProperties {
> private $__ro__bar;
> function __construct($val) {
> $this->__ro__bar = $val;
> }
> }
> $f = new foo(123);
> var_dump($f->bar);
> The special getter sees that bar isn't in the properties table and looks for
> __ro__bar instead (overriding visibility restrictions in the process).
> Since there's no write_property override, the prop remains unwritable from
> outside of the object.
> -Sara
Best regards,
Marcus