What exactly are annotations? How can they change the way you code and make life simpler? Annotations allow you to implement new functionality into code using "notes" this allows you to easily maintain your own architecture but benefit from external tools. Let's look at how annotations are used today in PHP and how we can develop our own solutions based on the existing libraries.
38. subject
-- In English --
An annotation is a note that is made while
reading any form of text.
39. -- In Code Speak --
“An annotation describes behavior of code and
affects your application in runtime.”
“Annotations do not directly affect program
semantics”
40. -- In Code Speak --
“An annotation describes behavior of code and
affects your application in runtime.”
“Annotations do not directly affect program
semantics”
just
484. use
AnnotationReader
?php
use DoctrineORMMapping as ORM;
use SymfonyComponentValidator
Constraints as Assert;
/**
* @ORMColumn(‘string’)
* @AssertNotBlank()
*/
492. use
AnnotationReader
?php
use DoctrineORMMapping as ORM;
use SymfonyComponentValidator
Constraints as Assert;
/**
* @ORMColumn(‘string’)
* @AssertNotBlank()
“metadata” */
new ORMColumn(‘string’)
new AssertNotBlank()
500. use
AnnotationReader
?php
use DoctrineORMMapping as ORM;
use SymfonyComponentValidator
Constraints as Assert;
/**
* @ORMColumn(‘string’)
* @AssertNotBlank()
“metadata” */
new ORMColumn(‘string’)
new AssertNotBlank()
Walker
536. annotation
{
public function __construct($options)
{
/* no options */
}
public function filter($value)
{
return str_replace(dogs, , (string) $value);
}
}
563. namespace Filter;
class Filter
{
private $reader;
public function __construct(DoctrineCommonAnnotationsReader $reader)
{
$this-reader = $reader;
}
public function filter($object)
{
$reflectionObject = new ReflectionObject($object);
foreach ($reflectionObject-getProperties() as $reflectionProperty) {
$this-filterProperty($object, $reflectionProperty);
}
}
public function filterProperty($object, $reflectionProperty)
{
// fetch the @NoDog annotation from the annotation reader
$annotation = $this-reader-getMethodAnnotation(
$reflectionProperty, 'FilterRuleNoDog');
if (null !== $annotation) {
return;
}
$reflectionProperty-setAccessible(true);
$value = $reflectionProperty-getValue($object);
$filteredValue = $annotation-filter($value);
$reflectionProperty-setValue( $filteredValue );
}
}
564. namespace Filter;
class Filter
{
private $reader;
public function __construct(DoctrineCommonAnnotationsReader $reader)
{
$this-reader = $reader;
}
public function filter($object)
{
$reflectionObject = new ReflectionObject($object);
foreach ($reflectionObject-getProperties() as $reflectionProperty) {
$this-filterProperty($object, $reflectionProperty);
}
}
Get
567. annotation
public function filterProperty($object, $reflectionProperty)
{
// fetch the @NoDog annotation from the annotation reader
$annotation = $this-reader-getMethodAnnotation(
$reflectionProperty, 'FilterRuleNoDog');
if (null !== $annotation) {
return;
}
$reflectionProperty-setAccessible(true);
$value = $reflectionProperty-getValue($object);
$filteredValue = $annotation-filter($value);
$reflectionProperty-setValue( $filteredValue );
}
}
568. namespace Filter;
class Filter
{
private $reader;
public function __construct(DoctrineCommonAnnotationsReader $reader)
{
$this-reader = $reader;
}
public function filter($object)
{
$reflectionObject = new ReflectionObject($object);
foreach ($reflectionObject-getProperties() as $reflectionProperty) {
$this-filterProperty($object, $reflectionProperty);
}
}
Get
571. annotation
public function filterProperty($object, $reflectionProperty)
{
// fetch the @NoDog annotation from the annotation reader
$annotation = $this-reader-getMethodAnnotation(
$reflectionProperty, 'FilterRuleNoDog');
if (null !== $annotation) {
return;
}
$reflectionProperty-setAccessible(true);
$value = $reflectionProperty-getValue($object);
Make