News for hackers. Stuff that matters.
Re:PHP Annotations Are a Horrible Idea by yuri41 on Wednesday October 17, 2012 @08:19AM (#4664030)
Closures inside property initializations inside the class body don't work. Not even in an array.

So this code throws up a "Parse error: syntax error, unexpected 'function' (T_FUNCTION)"

    class User
    {
        public static $mapping = array(
            'username' => array(
                'type' => 'string',
                'length' => 32,
                'unique' => true,
                'nullable' => function() {
                    // Some logic to determine value.  
                }
            )
        );  
    }
Re:PHP Annotations Are a Horrible Idea by stevencorona on Wednesday October 17, 2012 @08:22AM (#4664040)
Came here to post the same thing. I don't know if it has been fixed in 5.4, though.
Re:PHP Annotations Are a Horrible Idea by robryan on Wednesday October 17, 2012 @09:23AM (#4664261)
Nope, the parser seems to be pretty brittle around this. Someone who knows more about the internals can probably explain why this works:

  class User
  {
      public $mapping = array(
          'username' => array(
              'type' => 'string',
              'length' => 32,
              'unique' => true,
          )
      );  

      public function __construct()
      {
      	$this->mapping['nullable'] = function() { echo "test"; };
      	$this->mapping['nullable']();
      }
  }

  $user = new User();
And this doesn't:

  class User
  {
      public static $mapping = array(
          'username' => array(
              'type' => 'string',
              'length' => 32,
              'unique' => true,
          )
      );  

      public static function set()
  	{
  		self::$mapping['nullable'] = function() { echo "test"; };
  		self::$mapping['nullable']();
  	}
  }

  User::set();
Re:PHP Annotations Are a Horrible Idea by TazeTSchnitzel on Wednesday October 17, 2012 @10:43AM (#4664644)
I used to be interested in helping fix PHP, but I gave up.

The reason is because Rasmus and a few others seem to think that using the syntax definition to do your type checking for you is a good idea, and that the hundreds of possible cases they didn't think of, don't matter.

Re:PHP Annotations Are a Horrible Idea by snorkel on Wednesday October 17, 2012 @10:05AM (#4664457)
Don't need to inline the function, it could instead just be a reference to static member of the same class: ... 'nullable' => self::getNullable, ...
Re:PHP Annotations Are a Horrible Idea by pierredup on Friday October 19, 2012 @04:48AM (#4672750)
'nullable' => self::getNullable

also won't work in any version of PHP