Skip to content

Implement PHP attributes #1878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed

Implement PHP attributes #1878

wants to merge 10 commits into from

Conversation

dstogov
Copy link
Member

@dstogov dstogov commented Apr 20, 2016

No description provided.

@HallofFamer
Copy link

Umm what is this one? PHP native support for annotation?

@dstogov
Copy link
Member Author

dstogov commented Apr 20, 2016

Yes, but it's incomplete yet.

On Wed, Apr 20, 2016 at 4:16 PM -0700, "HallofFamer" <notifications@github.commailto:notifications@github.com> wrote:

Umm what is this one? PHP native support for annotation?

You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHubhttps://github.com//pull/1878#issuecomment-212648142

dstogov added 3 commits April 21, 2016 10:16
* master:
  Fixed bug #72059 - Reference the bug id directly in NEWS
  Fix magic constants (__LINE__) with ?? for constant scalar exprs
  Add NEWS entry for ?? in constant scalar expressions
  allow null coalescing (??) on constant expressions
@HallofFamer
Copy link

I see, so you decide to call it 'attributes', interesting. And the syntax for annotations is <>? How about just ? I doubt angle brackets are used in any existing PHP language construct yet.

@dstogov
Copy link
Member Author

dstogov commented Apr 21, 2016

I took syntax from HHVM: https://docs.hhvm.com/hack/attributes/introduction


From: HallofFamer notifications@github.com
Sent: Thursday, April 21, 2016 17:59
To: php/php-src
Cc: Dmitry Stogov; Author
Subject: Re: [php/php-src] Implement PHP attributes (#1878)

I see, so you decide to call it 'attributes', interesting. And the syntax for annotations is <>? How about just ? I doubt angle brackets are used in any existing PHP language construct yet.

You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHubhttps://github.com//pull/1878#issuecomment-212959453

dstogov added 4 commits April 21, 2016 22:42
* master:
  Added news entry
  Safe execution timeout handling.
* master:
  Take pi defs into account when propagating defs
  Make pi placement independent of phi placement
  Drop some unnecessary checks
  Introduce ZEND_BITSET_FOREACH macros
  Fixed bug #68658 (Define CURLE_SSL_CACERT_BADFILE)
  Fix cURL test to use local server instead of google
  Fixed bug #72035 php-cgi.exe fails to run scripts relative to drive root
  Fixed bug #72069 (Behavior \JsonSerializable different from json_encode)
  Fixed bug #72075 (Referencing socket resources breaks stream_select)
  Make zval_ptr_dtor / _zval_dtor_func more robust
  Adjust DFG allocation size
  Make MAY_BE_ERROR handling more accurate
  Fix escape sequences in pgsql.c
  Mark zend_timeout as noreturn
  Merge def and gen sets
  Fix constant expr coaleasce with protected mode opcache
* master:
  Skip test on ZTS build
  Fixed bug #72101 (crash on complex code)
  Fixed race conditions in test
- changed Reflection*::getAttributes() to always return arrays (empty array for entity without attributes)
- changed Reflection*::getAttributes() to always return arrays for attribute values (empty array for attributes without value, array with single element for attributes with single value, etc)
- disabled attribute names started with "__" prefix (they are reserved for special purpose)
@scaytrase
Copy link

scaytrase commented Apr 26, 2016

https://wiki.php.net/rfc/attributes

Despite of the fact the idea is borrowed from the hack, I'd personally (as a developer) prefer current annotation system over this implementation. The reason are:

  • Structural problem
    Hack annotation are not classes or even structs, thus are not structured. There is no built-in validation, no defaults, no semantics, no behavior
  • Namespacing problem

Is generated by the previous one.
The Doctrine replacement example shows it well. You should either try to mak ALL attributes as \Doctrine\ORM or to manually filter them all to avoid undefined class exception. Suggested attribute system lacks the namespacing to avoid these situations. I see the namespacing option within the RFC, but it does not sove the problem until attribute is a structural element (class, funciton, whatether)

  • Multiplication problem

Also generated by the first one. The problem is that in the moments you want to have two attributes, The RFC advice is to replace the content with array. The structural problem strongly prohibit this behavior as you should check all possible argument formats every time you work with this attributes.
Current annotation system, again, allows you to flexibly decide how to work with several annotations as the Doctrine reader provide you each annotation as a separate instance of some annotation class.

Implementing these feature gives us a simple but really powerfull feature - backward compatibility with the previous annotation format. What's left - is a matter of format, which could be converted automatically using current software readers and AST implementation

@dstogov
Copy link
Member Author

dstogov commented Apr 26, 2016

All these problem are solvable by higher layer (like it shown in "Doctrive" example). This RFC proposes just a base functionality in PHP core by design.

@pmukhin
Copy link

pmukhin commented Apr 26, 2016

Hack's developers offered very strange syntax for annotations. It looks like they did it because of that '@' is used to mute errors on functions. But I think this RFC should be based on bests existing implementations like Java's or maybe Python's. Moreover some very useful tools like Doctrine use Java-like syntax for years.

For example:

@ORM\Column(name="id", type="int")
@ORM\GeneratedValue()
private $id;

Looks much better and more acquainted for new developers than this:

<<@ORM\Column(name="id", type="int")>>
<<@ORM\GeneratedValue()>>
private $id;

One more strange thing in Hack is "short" syntax for closures:

($some) ==> { ... }

It is not that short actually...

@scaytrase
Copy link

scaytrase commented Apr 26, 2016

@dstogov The "Doctrine" example only show that all these problems should be solved in almost every attribute processor which allow multiple attributes extended by users.

The solution is to implement software attribute parser which takes FQCN from the attribute name (I'm not sure,but current examples show that it ignores use directives, so you should write fqcn each time you want to reference a class), takes the positional arguments from the attribute and tries to instantiate an attribute class there.

The question - where is the general difference with caching Doctrine Annotation Reader? Annotations are cached, so it reads them very quick, annotations are structured, multivalued. etc.

@dstogov
Copy link
Member Author

dstogov commented Apr 26, 2016

I'm tired to repeat - "Doctrine" is not the only use case for attributes, and I'm not going to reimplement Doctrine in C. This RFC provides base functionality for native attribute parsing and storage. Then it may be re-used for extensible passer, design by contract, verification, attribute system, flags for PHP compiler, etc.

@scaytrase
Copy link

Doctrine is NOT the USE case. doctrine\annotations is the implementation of annotation mechanism. It's already used in hundreds of software projects, uncluding symfony, zend framework, drupal.

https://www.versioneye.com/php/doctrine:annotations/references (it's the list of the projects, using this directly, and many others use it transitionally).

Making the proposed attributes implementation backward compatible with this mechanism:

  • Does not diverge with your ideas of re-using it wherever you want (flags, contracts, verification, etc)
  • Will be easier to transfer to (as classes will stay the same, use directives with stay the same, only attribute syntax changes and this could be do by machine)
  • Type-strict (attributes are typed, like annotations in Java)

dstogov added 2 commits May 10, 2016 12:14
* master: (146 commits)
  update NEWS
  update NEWS
  Revert "Fixed bug #71820 pg_fetch_object bind parameters before call constructor"
  Revert "Backport patch for bug #71820"
  Fixed bug #64524 Add intl.use_exceptions to php.ini-*
  Remove unnecessary strlen check
  Fixed test
  Fix include_once in phpdbg
  Strlen cleanup (additions for previous one fix)
  Fixed bug #72172 (zend_hex_strtod should not use strlen)
  Revert "Fixed bug #72170 (JsonSerializable may inc apply count without dec it)"
  Fixed bug #72170 (JsonSerializable may inc apply count without dec it)
  Add $http_response_header tests
  Fixed possible crash
  Reimplemented Bob's commit bac6fdb without insignificant renaming and white-space changes
  Fixed typo
  Revert "Refactor zval cleanup into single function"
  update NEWS
  reorder entry alphabetically
  Re-fix #72165
  ...
@linaori
Copy link

linaori commented May 18, 2016

Why can't we just have (something like) Java annotations in PHP? 😢

@nikic
Copy link
Member

nikic commented Jun 4, 2016

Closing as https://wiki.php.net/rfc/attributes has been declined, at least for now.

@nikic nikic closed this Jun 4, 2016
beberlei added a commit to beberlei/php-src that referenced this pull request Feb 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants