Re: [RFC] Simple Annotations

From: Date: Tue, 28 Jun 2016 23:52:40 +0000
Subject: Re: [RFC] Simple Annotations
References: 1 2 3 4 5 6 7 8  Groups: php.internals 
Request: Send a blank email to internals+get-94318@lists.php.net to get a copy of this message
On 06/28/2016 04:24 PM, Rasmus Schultz wrote:
This was just an example of a value object - for example: class HttpMethod {
     private $method;
     protected function __construct($method) {
         $this->method = $method;
     }
     public function getMethod() {
         return $this->method;
     }
     public static function get() {
         return new self("GET");
     }
     public static function post() {
         return new self("POST");
     }
     // ...
} You could picture using a flyweight constructor inside those factory methods maybe, and probably other patterns... I think there's plenty of cases for "named constructors", this is just the PHP variety of that. There are also cases (such as this one) where only certain constructions are permitted - allowing any string for HTTP method (e.g. wrong names, wrong case etc.) is prevented by protecting the constructor...
A possibly uglier but useful case would be where you have a large number of properties in an annotation and want to pass them in by name, but not using an anonymous array. (See also, my earlier Drupal examples.) To wit: <<Node::definition()>> class Node { public static function definition() {
    $def = new NodeDefinition();
    $def->name = "node";
    $def->label = "A Node";
    $def->addLink('foo', 'bar');
    // ...
    return $def;
} // The rest of the node class here. } $annotations = $class_reflection->getAnnotations(); print get_class($annotations[0]); // prints "NodeDefinition". Or would that fail because it's not returning a self instance, but of another class? Rasmus, let me know when you have a proposal together and I'll try to dig up time to try rendering Drupal annotations in it again. :-) --Larry Garfield

« previous php.internals (#94318) next »