> On 4 Nov 2014, at 10:54, Benjamin Eberlei <[email protected]> wrote:
>
> I agree with Stas that a much simpler approach is probably realistic.
>
> beginning pure speculation here, i see a short array like syntax like:
>
> [foo="bar", bar="baz", baz=["key": "value"]]
> function annotated_fn() {}
>
> Maybe even exactly short array syntax:
>
> ["foo"="bar", "bar"="baz", "baz"=["key": "value"]]
> function annotated_fn() {}
>
> Then $reflectionFunction->getAnnotations() returns an array. Various
> PHP/Userland libraries and frameworks can then stick whatever symantic on
> top that they want.
The approach for annotations that I like best would actually be not
annotations, but Python-style decorators. They’re very simple, but very
powerful. They would allow you to do annotations, but also add extra
functionality to functions.
In Python, a decorator is a function (or callable object), and you use one like
this:
@some_decorator(foo, bar)
def myfunc():
# function source code here
This is actually syntactic sugar for the following:
def myfunc():
# function source code here
myfunc = some_decorator(myfunc, foo, bar)
Which would be equivalent to the following PHP code:
$myfunc = function myfunc() {
# function source code here
};
$myfunc = some_decorator($myfunc, foo, bar);
Basically, Python decorators allow you to explicitly have a function modify a
new function before it’s declared. This is pretty useful: If your decorator
does nothing, it’s just an annotation. But you can also use it to add
functionality. For example, you might make a decorator that does something
before and after a function runs.
--
Andrea Faulds
http://ajf.me/
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php