Re: Annotations Request
| From: | Paweł Stradomski | Date: | Wed, 20 Aug 2008 18:38:57 +0000 |
| Subject: | Re: Annotations Request | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-40021@lists.php.net to get a copy of this message | ||
W liście Marcus Boerger z dnia środa, 20 sierpnia 2008:
> Hello Volodymyr,
> Basically there is no need for annotations in PHP unlike there is in Python
> for instance. However, while the following work:
> php -r 'class T { const C=42; var $p=T::C; } var_dump(new T);'
> php -r 'class T { const C=42; const D=T::C; } var_dump(new T);
> the next two do not:
> php -r 'class T { const C=42; var $p=T::C + 1; } var_dump(new T);'
> php -r 'class T { const C=42; const D=T::C + 1; } var_dump(new T);
>
> So you might want to have full support for evaluated consts/default values.
I don't really get your point. Annotations are not about constant values, but
about adding arbitrary attributes to classes and methods/functions.
A simple use case (in pseudocode):
class MyController extends BaseController {
// www.example.com/index
@allow('any')
@method('get')
public function index() {
}
// www.example.com/comment
@allow('any')
@method('post')
public function comment() {
}
// www.example.com/admin
@allow('admin')
@method('get')
public function admin() {
}
}
class BaseController {
public function _execute() {
$action = $this->parseTheUrl();
$method = new ReflectionMethod($this, $action);
if ($method->annotations('method') != $_SERVER['REQUEST_METHOD']) {
return $this->methodNotAllowed();
} elseif (! $this->currentUser->hasPrivilege($method->annotations('allow')) {
return $this->forbidden();
} else {
return $this->$action();
}
}
}
Python does not need annotations, as functions can have arbitrary attribute
set:
def a():
pass
a.x = 5
It also has very powerful mechanism of decorators, which are basically
higher-order functions applied to functions on their definition, but I don't
think Volodymyr asks for so much.
--
Paweł Stradomski