# Annotations Request

- Thread: https://externals.io/message/40013
- Original archive: https://news-web.php.net/php.internals/40013
- Started by Volodymyr Iatsyshyn on 2008-08-19T13:54:16+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #40013 — Volodymyr Iatsyshyn — 2008-08-19T13:54:16+00:00

URL: https://externals.io/message/40013#40013

Hi,

Is there a chance to have annotations like in Java or like .NET attributes?

Now, I use Java Annotations emulation, through Reflections and PHPDoc 
blocks. Example:

/** Annotation One*/
class DemoAnnotation1 extends C01t_Annotation
{
     public $value;
}

/** Annotation Two*/
class DemoAnnotation2 extends C01t_Annotation
{
     public $p1;
     public $p2;
}

class DemoClass
{
     const SOME_CONST = 5;

     /** @DemoAnnotation1(DemoClass::SOME_CONST + 1) */
     public $property1;

     /** @DemoAnnotation2(p1 => 1, p2 => 'Hello, Word!') */
     public $property2;
}

Thanks,
   Volodymyr Iatsyshyn

---

## #40015 — Marcus Boerger — 2008-08-20T14:46:16+00:00

URL: https://externals.io/message/40013#40015 · In reply to: #40013

Hello Volodymyr,

Tuesday, August 19, 2008, 3:54:16 PM, you wrote:

> Hi,

> Is there a chance to have annotations like in Java or like .NET attributes?

> Now, I use Java Annotations emulation, through Reflections and PHPDoc 
> blocks. Example:

> /** Annotation One*/
> class DemoAnnotation1 extends C01t_Annotation
> {
>      public $value;
> }

> /** Annotation Two*/
> class DemoAnnotation2 extends C01t_Annotation
> {
>      public $p1;
>      public $p2;
> }

> class DemoClass
> {
>      const SOME_CONST = 5;

>      /** @DemoAnnotation1(DemoClass::SOME_CONST + 1) */
>      public $property1;

>      /** @DemoAnnotation2(p1 => 1, p2 => 'Hello, Word!') */
>      public $property2;
> }

Basically there is no need for annotations in PHP unlike there is in Python
for instance. However, while the following work:
php -r 'class T { const C=42; var $p=T::C; } var_dump(new T);'
php -r 'class T { const C=42; const D=T::C; } var_dump(new T);
the next two do not:
php -r 'class T { const C=42; var $p=T::C + 1; } var_dump(new T);'
php -r 'class T { const C=42; const D=T::C + 1; } var_dump(new T);

So you might want to have full support for evaluated consts/default values.

The reason why we do not support this right now is a limitation in how
consts and default values get initialised.

Best regards,
 Marcus

---

## #40021 — pstradomski@gmail.com — 2008-08-20T18:38:57+00:00

URL: https://externals.io/message/40013#40021 · In reply to: #40015

W liście Marcus Boerger z dnia środa, 20 sierpnia 2008:
> Hello Volodymyr,

> Basically there is no need for annotations in PHP unlike there is in Python
> for instance. However, while the following work:
> php -r 'class T { const C=42; var $p=T::C; } var_dump(new T);'
> php -r 'class T { const C=42; const D=T::C; } var_dump(new T);
> the next two do not:
> php -r 'class T { const C=42; var $p=T::C + 1; } var_dump(new T);'
> php -r 'class T { const C=42; const D=T::C + 1; } var_dump(new T);
>
> So you might want to have full support for evaluated consts/default values.

I don't really get your point. Annotations are not about constant values, but 
about adding arbitrary attributes to classes and methods/functions. 

A simple use case (in pseudocode):

class MyController extends BaseController {

   // www.example.com/index
   @allow('any')
   @method('get')
   public function index() {
   }

   // www.example.com/comment
   @allow('any')
   @method('post')
   public function comment() {
   }

   // www.example.com/admin
   @allow('admin')
   @method('get')
   public function admin() {
   }
}

class BaseController {
   public function _execute() {
        $action = $this->parseTheUrl();
        $method = new ReflectionMethod($this, $action);
        if ($method->annotations('method') != $_SERVER['REQUEST_METHOD']) {
             return $this->methodNotAllowed();
	} elseif (! $this->currentUser->hasPrivilege($method->annotations('allow')) {
             return $this->forbidden();
	} else {
             return $this->$action();
        }
   }
}


Python does not need annotations, as functions can have arbitrary attribute 
set:

def a():
   pass

a.x = 5

It also has very powerful mechanism of decorators, which are basically 
higher-order functions applied to functions on their definition, but I don't 
think Volodymyr asks for so much. 

-- 
Paweł Stradomski

---

## #40040 — Volodymyr Iatsyshyn — 2008-08-21T08:45:08+00:00

URL: https://externals.io/message/40013#40040 · In reply to: #40021

Now I use emulation of Java Annotations. I wrote simple class, that uses 
PHPDoc block, gets annotations declarations and their arguments, uses 
eval() to convert arguments string to array, then I create instance of 
class and assigns parameters. This solution is fine just now, but it 
will be very cool to have annotations as language feature. I'm pretty 
sure that lots of frameworks would like this feature :-)

Paweł Stradomski wrote:
> W liście Marcus Boerger z dnia środa, 20 sierpnia 2008:
> > […]
> 
> > […]
> 
> I don't really get your point. Annotations are not about constant values, but 
> about adding arbitrary attributes to classes and methods/functions. 
> 
> A simple use case (in pseudocode):
> 
> class MyController extends BaseController {
> 
>    // www.example.com/index
>    @allow('any')
>    @method('get')
>    public function index() {
>    }
> 
>    // www.example.com/comment
>    @allow('any')
>    @method('post')
>    public function comment() {
>    }
> 
>    // www.example.com/admin
>    @allow('admin')
>    @method('get')
>    public function admin() {
>    }
> }
> 
> class BaseController {
>    public function _execute() {
>         $action = $this->parseTheUrl();
>         $method = new ReflectionMethod($this, $action);
>         if ($method->annotations('method') != $_SERVER['REQUEST_METHOD']) {
>              return $this->methodNotAllowed();
> 	} elseif (! $this->currentUser->hasPrivilege($method->annotations('allow')) {
>              return $this->forbidden();
> 	} else {
>              return $this->$action();
>         }
>    }
> }
> 
> 
> Python does not need annotations, as functions can have arbitrary attribute 
> set:
> 
> def a():
>    pass
> 
> a.x = 5
> 
> It also has very powerful mechanism of decorators, which are basically 
> higher-order functions applied to functions on their definition, but I don't 
> think Volodymyr asks for so much.

---

## #40041 — pstradomski@gmail.com — 2008-08-21T09:15:13+00:00

URL: https://externals.io/message/40013#40041 · In reply to: #40040

W liście Volodymyr Iatsyshyn z dnia czwartek 21 sierpnia 2008:
> Now I use emulation of Java Annotations. I wrote simple class, that uses
> PHPDoc block, gets annotations declarations and their arguments, uses
> eval() to convert arguments string to array, then I create instance of
> class and assigns parameters. This solution is fine just now, but it
> will be very cool to have annotations as language feature. I'm pretty
> sure that lots of frameworks would like this feature :-)
>
I understand, as I'm simulating those myself (although in a different way). 
But we'd love annotations for situations such as the one I presented before, 
as well as WSDL generation etc.

Instead of using annotations we use something like that:

class X {
   public static   myMethod_methods = array('POST', 'GET');
   public static   myMethod_access = 'any';
   public function myMethod() {....}
}

-- 
Paweł Stradomski

---

## #40080 — Volodymyr Iatsyshyn — 2008-08-25T12:24:27+00:00

URL: https://externals.io/message/40013#40080 · In reply to: #40013

Volodymyr Iatsyshyn wrote:
> Hi,
> 
> Is there a chance to have annotations like in Java or like .NET attributes?
> 
> Now, I use Java Annotations emulation, through Reflections and PHPDoc 
> blocks. Example:
> 
> /** Annotation One*/
> class DemoAnnotation1 extends C01t_Annotation
> {
>     public $value;
> }
> 
> /** Annotation Two*/
> class DemoAnnotation2 extends C01t_Annotation
> {
>     public $p1;
>     public $p2;
> }
> 
> class DemoClass
> {
>     const SOME_CONST = 5;
> 
>     /** @DemoAnnotation1(DemoClass::SOME_CONST + 1) */
>     public $property1;
> 
>     /** @DemoAnnotation2(p1 => 1, p2 => 'Hello, Word!') */
>     public $property2;
> }
> 
> Thanks,
>   Volodymyr Iatsyshyn

So is there a chance to have these?

---

## #40081 — Antony Dovgal — 2008-08-25T12:44:19+00:00

URL: https://externals.io/message/40013#40081 · In reply to: #40080

> So is there a chance to have these?

Volodymyr, there is very little sense in such questions.

If you really think feature XXX is that useful - care to demonstrate it's 
usefulness to others, "promote" it so that people start liking the idea of having it.
If you don't think you can manage writing the patch yourself, by promoting 
this feature you can find a person amongst the people who could do it for you.

In any case, the process of getting any feature implemented in PHP does not finish 
after someone says "ok, let's add it", in most cases there is a lot of work 
to be done, and writing a very detailed RFC in the Wiki would be very good start.

Also (I have to say it..) we do accept patches.

-- 
Wbr, 
Antony Dovgal
