annotating virtual properties?

18 views
Skip to first unread message

mindplay.dk

unread,
Jan 25, 2011, 8:07:15 AM1/25/11
to PHP Addendum library
Please take a look at the following simple example:

class Person
{
public function __get($name)
{
$fn = 'get'.ucfirst($name);
return $this->$fn();
}

public $birthday;

public function getAge()
{
return floor((time() - $this->birthday) / 24*60*60*365);
}
}

This class has a virtual property named "age" - rather than $person-
>getAge() you can simply say $person->age.

A lot of frameworks and ORMs add support for accessors, so this kind
of thing is common.

I can annotate the $birthday property with property-annotations - and
I can annotate the getAge() accessor method with method-annotations.

But my question is this: how do I annotate the virtual property "age"
with a property-annotation?

Since there is no declared $age member to annotate, how would you do
that?

In C# .NET they solved this issue using a rather lumpy approach - they
have a "metadata" annotation that basically tells the reflector to
report the properties and methods of one class as belonging to
another. In PHP it might look like this:

/** @metadata('PersonEx') */
class Person
{
// (see above...)
}

abstract class PersonEx
{
/** @type('int') */
public $age;
}

The PersonEx class is a dummy class - the metadata-annotation will
cause the reflector to report the $age property and it's annotations
as belonging to the Person class.

This solves the problem, but it's hardly an elegant solution.

Does Addendum deal with this issue?

aric caley

unread,
Jan 25, 2011, 12:46:32 PM1/25/11
to addendum...@googlegroups.com
I did something similar and my solution was to have in the constructor (in a base class all the other classes are derived from) some code that will use reflection to find the class properties and unset() them.  It grabs all the annotations on these properties and stows them for later use.  Then you can just declare all your properties normally and annotate them.  When you access those properties it falls through to the get*/set* methods just like you describe.


--
You received this message because you are subscribed to the Google Groups "PHP Addendum library" group.
To post to this group, send email to addendum...@googlegroups.com.
To unsubscribe from this group, send email to addendum-discu...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/addendum-discuss?hl=en.




--

Aric Caley | Project Manager, Technical Lead | After10studios
(p) 714.654.4859
(e) ar...@after10studios.com 
(w) www.after10studios.com


mindplay.dk

unread,
Jan 26, 2011, 6:40:41 PM1/26/11
to PHP Addendum library
Kind of hacky. Not more so than any other approach I've seen so far,
but not much better.

To me, half the idea with annotations, is repeating yourself less -
this approach (like others) forces you to repeat the property names.

Makes me wish PHP had actual accessors, and not just the hacky __get/
__set magic methods...
> > addendum-discu...@googlegroups.com<addendum-discuss%2Bunsubscrib e...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/addendum-discuss?hl=en.
>
> --
> *
> **Aric Caley | Project Manager, Technical Lead | A**fter10studios
> *(p) 714.654.4859
> (e) *a...@after10studios.com*
> (w)www.after10studios.com
Reply all
Reply to author
Forward
0 new messages