Live data from Hacker News

PHP Annotations Are a Horrible Idea

theunraveler.com

11–20 of 127 posts

Re: PHP Annotations Are a Horrible Idea

#11
Closures inside property initializations inside the class body don't work. Not even in an array.

So this code throws up a "Parse error: syntax error, unexpected 'function' (T_FUNCTION)"

    class User
    {
        public static $mapping = array(
            'username' => array(
                'type' => 'string',
                'length' => 32,
                'unique' => true,
                'nullable' => function() {
                    // Some logic to determine value.  
                }
            )
        );  
    }

Re: PHP Annotations Are a Horrible Idea

#12
post #5

This is pure horse pucky. First of all PHP isn't about elegance and I find it a teeth grinding experience when people complain about it not being so. PHP is about getting stuff done. He doesn't offer one good valid technical reason why not to use comments for meta-programming PHP other than "it just feels wrong" and an "icky feeling" which is entirely subjective. "Reliance on Yet Another Library"? C'mon. The fact tha…

I think this is quite a good technical reason:

    Because code comments should never be necessary for a script to function properly. 
In which other language do you see comment is necessary to make the code function properly?

Re: PHP Annotations Are a Horrible Idea

#13
What's in the annotation is configuration, not application logic. As such there's no real difference between putting configuration into YML, XML and annotation comments.

In response to your 4 specific criticisms: 1. (DX regressions) - in theory yes, in practice the frameworks are good at this. The annotations compile down to PHP which you can look at directly, and you get exceptions if there is something wrong with the annotation. 2. (Decreased readability) - subjective POV. I find it easier, because the relevant configuration is next to the code to which it applies. It also prevents crufty out-of-date comments floating around. 3. (Yet another library) - Yep. No answer there. 4. (Icky feeling) - I get far more icky feelings out of seeing 30 lines of custom written 'setup code'. Annotations help to prevent that.

Re: PHP Annotations Are a Horrible Idea

#14
post #5

This is pure horse pucky. First of all PHP isn't about elegance and I find it a teeth grinding experience when people complain about it not being so. PHP is about getting stuff done. He doesn't offer one good valid technical reason why not to use comments for meta-programming PHP other than "it just feels wrong" and an "icky feeling" which is entirely subjective. "Reliance on Yet Another Library"? C'mon. The fact tha…

I disagree.

> First of all PHP isn't about elegance...PHP is about getting stuff done.

What language isn't about getting stuff done?

> He doesn't offer one good valid technical reason why not to use comments...

Not true. The OP explicitly points out that it breaks many PHP debugging capabilities.

> The fact that you can even parse out comments with the reflection API lends, to me, that meta-programming is a-ok.

So what? PHP also has a goto function. Should we use it just because we can?

> I'll take the first one because I have other shit I need to do.

Just because it's less verbose doesn't mean it's better. Otherwise, everyone would use Coffeescript instead of JS.

Re: PHP Annotations Are a Horrible Idea

#15
post #5

This is pure horse pucky. First of all PHP isn't about elegance and I find it a teeth grinding experience when people complain about it not being so. PHP is about getting stuff done. He doesn't offer one good valid technical reason why not to use comments for meta-programming PHP other than "it just feels wrong" and an "icky feeling" which is entirely subjective. "Reliance on Yet Another Library"? C'mon. The fact tha…

the official RFC for annotations is actually a little cleaner in syntax, easier to read and implemented in the language itself which is a way beter system than comment annotations. On a level I do agree that docblock comments are no place for annotations, it's the best we can have for now though, just until annotations become a native PHP feature. Judging from the RCF and discussions on internals we still have a long…

> the official RFC for annotations is actually a little cleaner in syntax, easier to read and implemented in the language itself which is a way beter system than comment annotations.

Sure, everybody knows that, but when the language does not have actual annotations yet (in whichever form they are), in-comment annotations are a very common choice. That's also what Java libs tended to use before annotations were added in Java 5.

Re: PHP Annotations Are a Horrible Idea

#16
post #5

This is pure horse pucky. First of all PHP isn't about elegance and I find it a teeth grinding experience when people complain about it not being so. PHP is about getting stuff done. He doesn't offer one good valid technical reason why not to use comments for meta-programming PHP other than "it just feels wrong" and an "icky feeling" which is entirely subjective. "Reliance on Yet Another Library"? C'mon. The fact tha…

I think this is quite a good technical reason: Because code comments should never be necessary for a script to function properly. In which other language do you see comment is necessary to make the code function properly?

> In which other language do you see comment is necessary to make the code function properly?

If by "language" you mean "some library which also provides a separate but way more painful and verbose way of doing the same thing", then it existed in Java before Java 5 added annotations to the language itself.

Re: PHP Annotations Are a Horrible Idea

#17
post #11

Closures inside property initializations inside the class body don't work. Not even in an array. So this code throws up a "Parse error: syntax error, unexpected 'function' (T_FUNCTION)" class User { public static $mapping = array( 'username' => array( 'type' => 'string', 'length' => 32, 'unique' => true, 'nullable' => function() { // Some logic to determine value. } ) ); }

Came here to post the same thing. I don't know if it has been fixed in 5.4, though.

Re: PHP Annotations Are a Horrible Idea

#18

This could be solved if annotations were supported syntax. This works well in other languages (e.g. decorators in Python and annotations in Java)

And before annotations were added in java, the exact same hack was used for annotations. The alternative was to have separate configuration and mapping files which was painful. And then, you had libraries which did not have in-comment annotations and intermediate ones which compiled in-comment annotations to the original libraries's mapping files. Oh the fun we had.

Re: PHP Annotations Are a Horrible Idea

#19

> the PHP community is now officially so desperate for better metaprogramming capabilities that it is attempting to do so using code comments This says it all ... and to be honest, from an ease point of view I like using annotations but as with most parts of PHP it's the implementation that sucks (or the lack of implementation) Over 2 years ago a RFC has been filed for native php annotations and I must say that I lik…

That somebody could be you!

Re: PHP Annotations Are a Horrible Idea

#20
post #5

This is pure horse pucky. First of all PHP isn't about elegance and I find it a teeth grinding experience when people complain about it not being so. PHP is about getting stuff done. He doesn't offer one good valid technical reason why not to use comments for meta-programming PHP other than "it just feels wrong" and an "icky feeling" which is entirely subjective. "Reliance on Yet Another Library"? C'mon. The fact tha…

I think this is quite a good technical reason: Because code comments should never be necessary for a script to function properly. In which other language do you see comment is necessary to make the code function properly?

Its a syntax problem then ? what if the annotation wasn't inside comments would that make it ok ?
Post reply on HN