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.
Created
February 5, 2012 12:51
-
-
Save juzna/1745369 to your computer and use it in GitHub Desktop.
PHP annotations - experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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