Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:49759 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 48583 invoked from network); 17 Sep 2010 18:49:04 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Sep 2010 18:49:04 -0000 Authentication-Results: pb1.pair.com header.from=ralph@smashlabs.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=ralph@smashlabs.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain smashlabs.com from 67.15.58.61 cause and error) X-PHP-List-Original-Sender: ralph@smashlabs.com X-Host-Fingerprint: 67.15.58.61 openrce.org Linux 2.6 Received: from [67.15.58.61] ([67.15.58.61:38119] helo=users.smashlabs.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5A/A2-30333-D18B39C4 for ; Fri, 17 Sep 2010 14:49:01 -0400 Received: (qmail 11573 invoked from network); 17 Sep 2010 13:48:49 -0500 Received: from ip174-70-101-166.no.no.cox.net (HELO ralph-macbook.local) (174.70.101.166) by smashlabs.com with (AES256-SHA encrypted) SMTP; 17 Sep 2010 13:48:49 -0500 Message-ID: <4C93B81A.6040000@smashlabs.com> Date: Fri, 17 Sep 2010 13:48:58 -0500 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.9) Gecko/20061207 Thunderbird/1.5.0.9 Mnenhy/0.7.4.666 MIME-Version: 1.0 To: Stas Malyshev CC: Pierre Joye , "RQuadling@googlemail.com" , PHP internals References: <4C93AA3D.8070507@sugarcrm.com> In-Reply-To: <4C93AA3D.8070507@sugarcrm.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] On how a little knowledge is completely useless. From: ralph@smashlabs.com (Ralph Schindler) On 9/17/10 12:49 PM, Stas Malyshev wrote: > userspace) 2) adding syntax (probably can't be done in userspace unless > somebody thinks of a clever way to do it). I've thought of it, and I guess I'll give it a go, I could make this a proposal if it makes sense to. The idea is to make the Zend Engine aware that annotations exist without the worry of what the syntax is. Syntax can then be deferred to the consumer. How to accomplish? Well: * Zend Engine should have a new token T_ANNOTATION that is demarcated by an opening [ and a closing ] (with necessary escaping for \] along the way if need be) that should exists before PHP declarations (func, property, method, class, const, etc). Multiple annotations per declaration are ok. * Add new methods to Reflection APIs: getAnnotations() and getParsedAnnotations(). * Add a new extension called annotation responsible for registering and implementing annotation parsers: annotation_register_parser($name, AnnotationParser $p|$validcallback); ** and an interface for AnnotationParser interface AnnotationParser { public function tokenize($string); // array of tokens public function execute(array $tokens); } (if AnnotationParser is used, Reflection can "byte-code" cache the tokens from tokenize()) * Add real implementations of syntax: // class to handle proposed doctrine syntax, similar to c# class AnnotationAttributeParser { /** ... */ } // perhaps a simple key value based parser class AnnotationKeyValueParser { /** .. */ } // perhaps a PHP include parser class AnnotationPHPIncludeParser { /** .. */ } * Usage: annotation_register_parser('doctrine', new AnnotationAttributeParser); // callbacks can be used as wel annotation_register_parser('zfservice', array('MyClass', 'myCallbackMethod')); How would this work? When Reflection<>::getParsedAnnotations() is called, it will check for a registered key in the annotation (of one does not exist, exception is thrown).. code would look like this: [doctrine:ClassMap('foo' => 'bar', 'rest of c# nested syntax')] public $foo; This also allows other frameworks to propose and use their own syntax that makes the most sense to them: [symfonyDi:arg1=Foo,arg2=Bar] public function setStuff(Foo $arg1, Bar $arg2) {...} or [zendservice:param1=string;return=int] public function doFooForWebService() { /* .. */ return $someInt; } you could even use url based annotations: annotation_register_parser('wordpress', 'parse_str'); in code: [wordpress:first=value&arr[]=foo+bar&arr[]=baz] function wp_plugin_handle_something() {} Since annotation formats and knowing annotations might be required are a requirement of the framework/system the consumer has adopted, that framework can specify the format of the string used inside the annotation. Also, multiple annotations can be attached to various declarations, hence the getParsedAnnotations(), perhaps getParsedAnnotation($index) would be valid as well. thoughts? -ralph