rfc:attributes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:attributes [2016/04/28 19:46] – dmitry | rfc:attributes [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Attributes ====== | ====== PHP RFC: Attributes ====== | ||
- | * Version: | + | * Version: |
* Date: 2016-04-21 | * Date: 2016-04-21 | ||
* Author: Dmitry Stogov, dmitry@zend.com | * Author: Dmitry Stogov, dmitry@zend.com | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
Line 14: | Line 14: | ||
==== Attribute Syntax ==== | ==== Attribute Syntax ==== | ||
- | < | + | < |
<code php> | <code php> | ||
Line 22: | Line 22: | ||
</ | </ | ||
- | Each attribute definition construct may also define one or more named attribute, which may be used with no value, a single value or multiple values. | + | Each attribute definition construct may also define one or more named attribute, which may be used with no value, a single value or multiple values. |
< | < | ||
< | < | ||
{ "," | { "," | ||
- | < | + | <namespace-name> |
< | < | ||
</ | </ | ||
Line 72: | Line 72: | ||
==== Reflection ==== | ==== Reflection ==== | ||
- | Reflection classes are extended with the getAttributes() | + | Reflection classes are extended with the getAttributes() |
<code php> | <code php> | ||
Line 182: | Line 182: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | [[https:// | ||
==== Use Cases ==== | ==== Use Cases ==== | ||
- | With attributes it's extremely simple to mark some functions with some specific | + | With attributes, it's extremely simple to mark some functions with some specific |
<code php> | <code php> | ||
Line 199: | Line 201: | ||
</ | </ | ||
- | Attributes may be used to provide | + | Attributes may be used as a base level for an annotation system similar to Doctrine, where each attribute is represented by an object of corresponding class that perform validation and other actions. |
<code php> | <code php> | ||
Line 266: | Line 268: | ||
==== Special Attributes ==== | ==== Special Attributes ==== | ||
- | Attribute names starting with " | + | < |
- | ==== Criticism and Alternative | + | ==== Criticism and Alternative |
+ | |||
+ | === Doc-comments | ||
Today we are using single doc-comments for any kind of meta-information, | Today we are using single doc-comments for any kind of meta-information, | ||
Line 309: | Line 313: | ||
It might be possible to make PHP parse existing doc-comments and keep information as structured attributes, but we would need to invoke additional parser for each doc-comment; | It might be possible to make PHP parse existing doc-comments and keep information as structured attributes, but we would need to invoke additional parser for each doc-comment; | ||
+ | |||
+ | === Full Featured Annotation System (like Doctrine) === | ||
+ | |||
+ | This RFC proposes only base PHP attribute functionality. It doesn' | ||
+ | |||
+ | <code php> | ||
+ | /** | ||
+ | * @Block( | ||
+ | * id = " | ||
+ | * | ||
+ | * ) | ||
+ | */ | ||
+ | |||
+ | << | ||
+ | " | ||
+ | " | ||
+ | ]))>> | ||
+ | class PageTitleBlock { | ||
+ | } | ||
+ | |||
+ | function TranslateDrupalAttribute($value) { | ||
+ | if ($value instanceof \ast\Node) { | ||
+ | if ($value-> | ||
+ | $a = $value-> | ||
+ | if (is_string($a) && class_exists($a)) { | ||
+ | $value = new $a; | ||
+ | } else if ($a instanceof \ast\Node && | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | $args = $a-> | ||
+ | if ($args instanceof ast\Node && $args-> | ||
+ | $obj = new $a-> | ||
+ | foreach ($args-> | ||
+ | if ($arg instanceof ast\Node && | ||
+ | $arg-> | ||
+ | count($arg-> | ||
+ | is_string($arg-> | ||
+ | $name = $arg-> | ||
+ | $val = $arg-> | ||
+ | if ($val instanceof ast\Node) { | ||
+ | $obj-> | ||
+ | } else { | ||
+ | $obj-> | ||
+ | } | ||
+ | } else { | ||
+ | throw DrupalAnnotationError(" | ||
+ | } | ||
+ | } | ||
+ | } else { | ||
+ | $name = $a-> | ||
+ | $obj = new $name($args); | ||
+ | } | ||
+ | $value = $obj; | ||
+ | } else { | ||
+ | throw DrupalAnnotationError(" | ||
+ | } | ||
+ | } else { | ||
+ | throw DrupalAnnotationError(" | ||
+ | } | ||
+ | } | ||
+ | return $value; | ||
+ | } | ||
+ | |||
+ | function GetDrupalAnnotations($class_name) { | ||
+ | $reflClass = new \ReflectionClass($class_name); | ||
+ | $attrs = $reflClass-> | ||
+ | $ret = []; | ||
+ | foreach ($attrs as $name => $values) { | ||
+ | if ($name == " | ||
+ | foreach ($values as & | ||
+ | $ret[] = TranslateDrupalAttribute($value); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | return $ret; | ||
+ | } | ||
+ | |||
+ | class Block {} | ||
+ | class Translation { | ||
+ | public $text; | ||
+ | function __construct($text) { | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | var_dump(GetDrupalAnnotations(" | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | array(1) { | ||
+ | [0]=> | ||
+ | object(Block)# | ||
+ | [" | ||
+ | string(21) " | ||
+ | [" | ||
+ | object(Translation)# | ||
+ | [" | ||
+ | string(13) "Site branding" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === ' | ||
+ | |||
+ | ' | ||
+ | |||
+ | === Naming (attributes or annotations) === | ||
+ | |||
+ | Different programming languages use different terms for similar features. Some use annotation, some attributes. I prefer name " | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 337: | Line 456: | ||
* < | * < | ||
* < | * < | ||
- | * It may be useful to optionally allow some extra special character e.g. < | + | * <del>It may be useful to optionally allow some extra special character e.g. < |
* < | * < | ||
- | * Naming: " | + | * <del>Naming: " |
| | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | This RFC modifies the PHP language syntax | + | The voting started on May 10th, 2016 and will close on May 24th, 2016. |
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | ---- | ||
- | In addition, allow <php-expression> | + | <doodle title=" |
+ | * valid PHP expression (internally represented as AST) | ||
+ | * valid PHP constant (number or string) | ||
+ | </ | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
Line 358: | Line 487: | ||
* [[https:// | * [[https:// | ||
* [[https:// | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
- | ===== Rejected Features ===== | ||
- | Keep this updated with features that were discussed on the mail lists. |
rfc/attributes.1461872801.txt.gz · Last modified: 2017/09/22 13:28 (external edit)