Skip to content

Instantly share code, notes, and snippets.

@juzna
Created February 5, 2012 12:51
Show Gist options
  • Save juzna/1745369 to your computer and use it in GitHub Desktop.
Save juzna/1745369 to your computer and use it in GitHub Desktop.
PHP annotations - experiment
<?php
#
@Pepa\B\C
@NovakA(A)
@NovakB("B")
@NovakC(a="b",c="d",e="f")
class Pepa {
@NovakConst
const myConst = 1;
@NovakProp
public $prop;
@NovakFceX @NovakFceX2
public function x() {
echo "X\n";
}
public static function y() {
echo "Y\n";
}
}
$pepa = new Pepa();
$pepa->x();
$cls = new ReflectionClass('Pepa');
var_dump($a = $b = $cls->getAnnotations());

Annotation character

In PHP, @ character is used as silence operator and thus cannot be reused for annotations - PHP wouldn't know whether it's an annotation or silenced expression (you may guess it from the code, but it has to be obvious soon enough, i.e. immediately after the at sign). I experimented with # character to introduce annotations, which are then defined by normal @ char. When annotating methods or properties, the collision doesn't happen and therefore # is not needed.

Pepa has got annotations
array(4) {
[0] =>
string(8) "Pepa\\B\\C"
[1] =>
array(2) {
'name' =>
string(6) "NovakA"
'params' =>
array(1) {
'value' =>
string(1) "A"
}
}
[2] =>
array(2) {
'name' =>
string(6) "NovakB"
'params' =>
array(1) {
'value' =>
string(1) "B"
}
}
[3] =>
array(2) {
'name' =>
string(6) "NovakC"
'params' =>
array(3) {
'a' =>
string(1) "b"
'c' =>
string(1) "d"
'e' =>
string(1) "f"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment