-
-
Save mindplay-dk/ebd5e4f7da51da3c4e56232adef41b46 to your computer and use it in GitHub Desktop.
<?php | |
class Route | |
{ | |
public $pattern; | |
public $method; | |
public function __construct($pattern, $method) | |
{ | |
$this->pattern = $pattern; | |
$this->method = $method; | |
} | |
public static function get($pattern) { | |
return new self($pattern, "GET"); | |
} | |
} | |
class Filter | |
{ | |
public $name; | |
public function __construct($name) | |
{ | |
$this->name = $name; | |
} | |
} | |
class UserController | |
{ | |
<< Route::get('^/users/\w+$') >> | |
<< new Filter("membership") >> | |
public function showProfile() | |
{ | |
// ... | |
} | |
} | |
$method = new ReflectionMethod(UserController::class, "showProfile"); | |
var_dump($method->getAnnotations()); // => array(0 => {Route}, 1 => {Filter}) | |
var_dump($method->getAnnotations(Filter::class)); // => array(0 => {Filter}) |
<?php | |
// C# style annotation rules can be implemented entirely in userland | |
class Usage | |
{ | |
public $where; | |
public function __construct($where) { | |
$this->where = $where; | |
} | |
public static $classes = new Usage("classes"); | |
public static $methods = new Usage("methods"); | |
public static $properties = new Usage("properties"); | |
public static $multiple = new Usage("multiple"); | |
public static $inherit = new Usage("inherit"); | |
} | |
class Annotations | |
{ | |
public function getFromClass($class) { | |
// get class reflection | |
// collect annotations from class and recursively from parents | |
// for each annotation | |
// get Usage annotations from the annotation-class | |
// validate usage | |
// validate cardinality (single or multiple) | |
// apply inheritance logic (filter overridden annotations) | |
// return annotations | |
} | |
public function getFromMethod($class, $method) { | |
// ... | |
} | |
public function getFromProperty($class, $property) { | |
// ... | |
} | |
} |
For anyone who hate this syntax, solve this problem and then you can continue argue:
$fn = <<new Annotation()>> function () { /** ... */ };
@fesor there is simple solution for this problem - abandon @
silent operator for error supression.
It's a huge BC break but IMHO using error supression provides more complexity and using exceptions instead would provide clean way for handling errors there is try/catch for that - that would be awesome change in my opinion.
The STFU operator @
should be considered as bad practice, IMHO those usages should be replaced with valid exception handling. Removing STFU could be marked as deprecated and then some users would reflect and refactor for right way of exception handling but also part of users would just blow deprecation errors by adding additional STFU which might cause unproper usage of $ret = @@foo();
.
Maybe I'm just thinking loud and that's all.
I agree <<>> is ugly, I much prefer @ or @@
using @ to suppress errors is super bad in my opinion, I last used that in 2002 when I was young and stupid, I now handle that error instead of sweep it under the rug.
I'm honestly ok with busting BC as if someone is going to upgrade to a new version of PHP, they should expect legacy code to ge a little bruised up in the process and its more of knowing whats going to break before upgrading your system. especially if this makes the cut for PHP 8, I'd like to see a lot of things get disrupted to clean up the API finally.
Why this ugly syntax again? I mean "<< >>"? Is it really important to keep compatibilty with HHVM?
There's a lot of languages with native annotations built in such as java, python... Does it look any worse?
Than this one:
Aren't you (hhvm team in the reality) inventing a wheel inventing new syntax for annotations?