[PHP-DEV] Re: Annotations Request

2008-08-25 Thread Volodymyr Iatsyshyn

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?

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP-DEV] Re: Annotations Request

2008-08-21 Thread Volodymyr Iatsyshyn

Lupus Michaelis wrote:

Volodymyr Iatsyshyn a écrit :

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


  Can you provide an URL that explain what is annotation ?
Your examples don't help me, so I guess I'm not alone.



http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Annotations Request

2008-08-21 Thread Volodymyr Iatsyshyn
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:

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. 



--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP-DEV] Annotations Request

2008-08-20 Thread Volodymyr Iatsyshyn

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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php