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
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
, 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.
, we are taking the same approach to add meta-information to our classes but on a more generic level. Through our
, 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:
- Performance, I guess the parser code will be a lot faster in C that in PHP
- $ php -w will strip the comments, which should not hurt an application
- Make it a standard, as Stubbles will probably never be the standard framework for everybody
There currently are several applications, that rely on PHP's faked annotations, they are used to emulate the popular WebMethod, that's available in
ASP.NET and
Java, for dependency injection, Object-Relational-Mapping and a lot of other tasks, I forgot to mention.
So it would definitely be worth to include this feature into PHP, even it is already available in Java

. Splitting annotations in an interface and an implementation, PHP annotations would even be a lot more powerful, then they are in other langauges. This way they could be used to inject arbitrary code in your method calls, so they'd even allow you to take a small step in the direction of AOP.
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