Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:40040 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 95085 invoked from network); 21 Aug 2008 08:45:12 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Aug 2008 08:45:12 -0000 X-Host-Fingerprint: 194.44.247.140 unknown Received: from [194.44.247.140] ([194.44.247.140:21116] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FA/82-06543-61B2DA84 for ; Thu, 21 Aug 2008 04:45:12 -0400 Message-ID: To: internals@lists.php.net Date: Thu, 21 Aug 2008 11:45:08 +0300 User-Agent: Thunderbird 2.0.0.16 (Windows/20080708) MIME-Version: 1.0 References: <8C.C1.14056.B80DAA84@pb1.pair.com> <1586387787.20080820164616@marcus-boerger.de> <200808202038.57642.pstradomski@gmail.com> In-Reply-To: <200808202038.57642.pstradomski@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Posted-By: 194.44.247.140 Subject: Re: [PHP-DEV] Annotations Request From: viatsyshyn@gmail.com (Volodymyr Iatsyshyn) Now I use emulation of Java Annotations. I wrote simple class, that uses PHPDoc block, gets annotations declarations and their arguments, uses eval() to convert arguments string to array, then I create instance of class and assigns parameters. This solution is fine just now, but it will be very cool to have annotations as language feature. I'm pretty sure that lots of frameworks would like this feature :-) Paweł Stradomski wrote: > 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. >