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
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php