4

Last time i checked (2008) annotations in php weren't so wide spread. Now after reading some material and having done some "googling", i am still a little confused. Could someone provide a minimal working example that illustrates how to use annotations in php:
Lets just say i want something like this to work:

class ClassName
{
  /**
  * @DefaultValue("test")
  */
  public $prop;
}

// so that i can do 

$kls = new ClassName();
$kls->prop // => test

I havent done php in a long while

UPDATE
The purpose of this question is to understand how libraries/frameworks like symfony,flow3 and doctrine implement their annotations.

krichard
  • 3,699
  • 24
  • 34

5 Answers5

4

Annotations are not (yet) supported by PHP. There is an RFC for a proposed implementation, but it's still unknown if or when would that happen.

The particular example that you've given might be interpreted by an IDE as a value to auto-complete and otherwise you can just do public $prop = 'Test';, but I guess you already know that and it's not your real intention.

Narf
  • 14,600
  • 3
  • 37
  • 66
2

In PHP Manual -> Function Reference Variable and Type Related Extensions -> Reflection, exists a method getDocComment() in several reflection classes (ReflectionClass, ReflectionMethod, ReflectionFunction, ReflectionFunctionAbstract, ReflectionProperty...) where you can get the doc comment block. Then process your comment the way you want. Example:

class Foo {
   private $a;
   private $b;
   ...
   /**
    * @annotation('baz')
    */
   public function bar() {
      ...
   }
}

$reflMethod = new ReflectionMethod('Foo', 'bar');

//prints 
//string(26) "/**
// * @annotation('baz')
// */"
var_dump($reflMethod->getDocComment());
Mauro
  • 31
  • 2
1

PHP Shouldn't support annotations, they're a scourge among PHP developers at the moment. They cause many problems:

  1. They break version control. If you want two differently configured instances of an object in different projects they become different branches even though it's the configuration that's changed and not the logic.

  2. They reduce portability. If you move a class around between two projects that use annotations, the annotations may break the second project

  3. They break encapsulation. The application code shouldn't be concerned with how it will be used externally. Annotations tell the client code what it should be doing rather than letting the client code decide. In your @DefaultValue example, what if you use the class somewhere that isn't paying attention to the annotation? Will the class work anyway if the default value hasn't been set? Yes? No? Maybe? Whatever the answer, it's not clear in the API and there's no way to know whether the object, once constructed is ready to fulfil its responsibilities.

  4. They limit flexibility. Using Symfony's @Inject as an example, it's impossible to create two instances of the class with different injected parameters .

See: Annotations are an abomination and PHP Annotations Are a Horrible Idea for a more detailed explanation of why they should be avoided.

Tom B
  • 2,735
  • 2
  • 24
  • 30
  • 6
    I guess you are missing the point of the question. [krichard](http://stackoverflow.com/users/515399/krichard) asked for " _minimal working example that illustrates how to use annotations in php_ " and not for personal opinions regarding the use of annotations in PHP. Discussing if is OK to use annotations in PHP feels like old " _4 space vs 2 space indent_ " debate. – Alexandru Guzinschi Aug 20 '13 at 08:25
  • Sometimes people ask the wrong question because they believe it's the best way of achieving what they're looking for. It's almost always better to steer them in the right direction. E.g. "How can I use global variables here?" or "How can I make this a singleton?" are an equally valid questions. But the best answers would be ones explaining the better alternatives and why they're better than teaching people bad practices just because they asked to be taught them. Spaces have no practical benefit, annotations (for configuration) have several major drawbacks. – Tom B Aug 21 '13 at 09:19
  • I see your point and yes, in some situations it is better to diverge just to point to the right direction. But I see annotations more as a personal ( _or team_ ) decision and delight and not as a "really bad idea". – Alexandru Guzinschi Sep 03 '13 at 11:43
  • Annotations in this manner cause more than enough problems to warrant avoiding them completely. They add one single benefit: letting the developer edit two sets of data in one file, compared to the drawbacks this causes it's definitely a "really bad idea". Global variables/singletons/static dependencies are also "personal/team" decisions but equally really bad ideas and I'd expect to see answers to questions on those steering people towards better alternatives rather than simply explaining how to use them. – Tom B Sep 04 '13 at 16:25
0

PHP doesn't support annotations (yet).

The only place I've used annotations in PHP that affect code flow is in phpUnit, which supports a number of annotation markers (eg @DataProvider and @ExpectedException) for handling various aspects of the test script.

That works fairly well, but isn't handled by PHP natively; phpUnit has to parse the script itself before including it and running the code normally. Fair enough for a unit test script, but not an ideal solution for a production system.

So I think the real answer is that you'll have to wait until PHP implements annotations natively. I believe there's a proposal for it, but it certainly isn't going to happen any time soon -- it's definitely not going to be in 5.5, which will be released next year. I don't think there are any fixed plans for the features in 5.6 or beyond, but if we're stretching out into at least 2014 for that, even assuming your hosting provider or server admins are willing to upgrade immediately.

SDC
  • 14,192
  • 2
  • 35
  • 48
0

Yes, PHP doesn't support annotations yet, but we can rather use Doctrine Annotations. Have a look - https://www.doctrine-project.org/projects/annotations.html

R Sun
  • 1,353
  • 14
  • 17