Cover image for post "Virtual properties"

"Virtual properties"

Jeff Moore posted an article on procata.com about getters, setters and real properties. I fully agree with him. Especially the usage of interceptors (__get()/__set()/__isset()/__call()) makes your API a lot more readable and comfortable, while maintaining the purpose behind getters and setters: Checking the correctness of values assigned to a property and wrapping around retrieval mechanisms for a property. I personally call the way of maintaining value-correctness for properties through interceptors virtual properties, which fits quite nice I think.

In eZ Components we make heavy use of this methodology, especially with our option classes. This allows nice looking code like:

$dialog->options->text = "Test text";

While the $dialog object ensures that it's $options property holds nothing but instances of the option class. The latter one ensures, that its $text property has a correct value at any time. If this is violated anywhere, an exception is thrown. I think this is much more intuitive and comfortable than

$dialog->getOptions()->setText( "Test text" );

If you are interessted in this topic, you should buy the upcoming issue of the German "PHP Magazin", which will contain an article about these techniques and some more advanced.

Comments

Hi,

while interceptors or not is imho more or less a religious question, I personally don't like them. The main reason is that my IDE (Eclipse/Zend) doesn't complete my code anymore and I have to lookup everything in the (anyhow great) documentation :) But maybe there's a way to fake it for code inspection IDEs ...

Soenke at 2007-05-08

phpDocumentor supports documenting virtual properties since some time. If you enhance your documentation, there is a rare chance that Zend IDE can parse the docs and do the completion for you. ;)

Toby at 2007-05-08

I believe you misread the post you are referencing. The blog belongs to a fellow by the name of Jeff Moore and he wrote the original article. I made the first comment to it, which actually stated that I did not care for the approach he described and listing reasons why. Please update your post accordingly to properly attribute the original article. Thanks.

Matthew Turland at 2007-05-08

Damn, you are so right. Dunno where I got the name from. Thanks for the hint, I fixed it immediatelly.

Toby at 2007-05-09

I have to agree with Soenke - although it's possible to document virtual properties for use with phpDocumentor, code completion in IDEs is still a big issue.

I have only tried this in Zend Studio, but the @var tag definitely doesn't work for code completion there. As the Eclipse-based PHPIDE project will undoubtedly inherit a lot of Zend Studio's parsing code, I doubt it will be any better in that regard.

So while I agree that interceptors are a cool and convenient way to solve a lot of problems, virtual properties and methods can also decrease developer productivity when you're used to having thorough code completion.

Markus Wolff at 2007-05-09

I'm actually unconcerned by the ZDE problem, since I'm using VIM. ;)

Toby at 2007-05-09

Somehow I knew you were gonna say that :-D

Markus Wolff at 2007-05-09

Yes? Why? ;)

But joke aside, I actually think virtual properties are a great way of managing the getter/setter dilemma in a very elegant way. Therefore, and because PHP documentation tools already support this way of API design, the major IDE vendors should adopt it and also provide completion features for it.

Or am I wrong?

Toby at 2007-05-09

It's depends. Sometimes better used first, sometimes second code.

sf at 2007-05-11