Re: Annotation PHP 7
| From: | Andrea Faulds | Date: | Tue, 04 Nov 2014 16:34:20 +0000 |
| Subject: | Re: Annotation PHP 7 | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-78648@lists.php.net to get a copy of this message | ||
> On 4 Nov 2014, at 10:54, Benjamin Eberlei <kontakt@beberlei.de> 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/