Sunday, February 25. 2007
My wishlist for PHP 6, pt3: Annotations
Porting JavaDoc comments to PHP was one of the best things, that ever happened to to PHP4. But like in Java 4, the DocBlocks in PHP evolved from plain documentation to a feature that adds meta information to classes, methods, properties and variables. IDEs, like Zend Studio, use the @var tag to enable type hinting for method return values, which would not possible without the DocBlock, as PHP is a dynamic languages. Furthermore, there are projects like Services_Webservice or SCA, which add custom tags to the docblock to mark a method or class as a service and allow you to generate WSDL and a full-blown webservice server without the need to write additional code.
In Stubbles, we are taking the same approach to add meta-information to our classes but on a more generic level. Through our extended reflection API, we offer the ability to add annotations to classes, properties, methods or functions.
These annotations must have the following format:
@XMLTag(tagName=fooBar)This annotation is used in the XMLSerializer, to define how an object, property or method return value should be marshalled into XML. So it can be used like this: <?php
/**
* Simple test class
*
* @XMLTag(tagName=fooBar)
*/
class Foo {}
?>To be able to extract this information, you need to define a class named stubXMLTagAnnotation, that provides a setTagName() method: <?php
class stubXMLTagAnnotation extends stubAbstractAnnotation implemements stubAnnotation {
private $tagName;
public function setTagName($tagName) {
$this->tagName = $tagName;
}
public function getTagName() {
return $this->tagName;
}
public function getAnnotationTarget() {
return stubAnnotation::TARGET_CLASS;
}
}
?>Now you are all set, to fetch the information stored in the @XMLTag annotation of the class Foo by using the stubReflection API: <?php
$clazz = new stubReflectionClass('Foo');
$xmlTag = $clazz->getAnnotation('XMLTag');
print "The name of the tag should be " . $xmlTag->getTagName();
?>Using this API, it is very easy to access the meta information, without having to worry about parsing the DocBlocks yourself. Still, I would be very happy to see built-in annotation support in PHP6, that does not use comments, but allows us to write code like this: <?php
@XMLTag(tagName='FooBar');
class Foo {}
?>To define the annotation, we would use code like this: <?php
annotation class XMLTag {
public function setTagName($tagName) {...}
public function getTagName() {... }
}
?>The keyword annotation is used to mark this class as an annotation class so the parser knows, that it can be used as an annotation. The same should be available for interfaces, so we could use an interface name as the annotatation name and provide different implementations for the same annotation: <?php
annotation interface XMLTag {
public function setTagName($tagName);
public function getTagName();
}
// this will work as the example above
annotation class SimpleXMLTag implements XMLTag {
public function setTagName($tagName) {...}
public function getTagName() {...}
}
// this will return a random tag
annotation class RandomXMLTag implements XMLTag {
public function setTagName($tagName) {//no code needed here}
public function getTagName() {// will return any random string}
}
?>To specify the concrete implementation of the annotation, I'd recommend the following syntax: @XMLTag[RandomXMLTag]
@XMLTag[SimpleXMLTag](tagName='FooBar')As Stubbles already provides all of the above features, you might wonder, why I want this in PHP 6. Here are some of the reasons:
So it would definitely be worth to include this feature into PHP, even it is already available in Java ![]() ![]() Comments
Display comments as
(Linear | Threaded)
Still on the Java Lite path? There are other (and more powerfull) ways of doing meta programming besides annotations (just look at Ruby).
Since the core PHP team already has stated there will be no major engine additions other than those already presented a more realistic solution would be adding a docblock/annotation parser to the Reflection API.
I'm also pretty sure, that this will not be added in PHP 6, that's why I call it my "wish-list"
I'll promise to take a closer look at meta programming in Ruby. Whenever I saw some Ruby code, I just thought, it looks ugly...
Been living under the curse of C-style syntax for too long?
Because some people want to add good things that have already been introduced in other languages, doesn't mean it's bad. I love annotations as a way to metadata code. I know it can be done in userspace, but havining it native is much faster and defines a standard way. xml parsers can also be written in userspace.......
Of course adding features from other languages isn't bad per definition. However, adding a feature introduced to work around problems related to static typing to a dynamic language like PHP might not be so good. Java annotations is such a feature.
I'd like to see addition of more general meta programming features rather than an annotations exclusively construct. To define a standard way an annotations implementation utilizing these new meta programming features could be added to the PHP core library. Oh, and if performance is your primary concern.... why don't you stick to assembly?
I love annotations! This post was one of the many I referenced early on, when I started designing my own library.
In my opinion, the mistake made by you and countless others, is to invent new syntax. Although your syntax is closer to standard PHP-DOC syntax than some libraries, you've added custom syntax for properties and values. PHP has (familiar and readily available) syntax for that already. Okay, so you saved a few keystrokes - but you lost all of your standard PHP language features. The only new syntax you should need, is @name... everything you're trying to do between the parentheses, PHP already has syntax for - and the rest is PHP-DOC, an already well-defined, widely accepted and broadly supported standard, plus a pair of parentheses. Have a look at my project's Wiki for more information: http://code.google.com/p/php-annotations ![]() ![]() ![]() |
![]() ![]() Calendar
![]() Archives![]() Categories![]() Quicksearch![]() Syndicate This Blog![]() Blog Administration![]() ![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||





In part three of his wishlist for PHP 6 Stephan wrote that he would like to see annotations built into PHP 6 directly. I disagree with him about that. Annotations can be done in userland, without any problems. He already gave some examples of projects tha
Tracked: Feb 25, 15:42