0

I have problem with closure in PHP 5.4

I have array

public function check(){
return ['int'=>['filter'=>2],
'min'=>function($val){
return ['int'=>2,'min'=>$val];
}
]
}

When I use

(new Obj())->check()['int'];

it works. But I don't know how use min with parameter for example 3

I tryed

(new Obj())->check()['min'](3);
(new Obj())->check()['min'(3)];
(new Obj())->check()['min(3)'];

don't work.

110precent
  • 322
  • 4
  • 19
  • 1
    The most valid way is `(new Obj())->check()['min'](3);` - what is your result ? Any error ? – hsz Jul 15 '13 at 09:16
  • @hsz: That would be valid and correct in a language with a formal grammar. PHP is not that language. – Jon Jul 15 '13 at 09:20
  • Parse error: syntax error, unexpected '(' Buy in real enviroment I use `(new Valid())->checkVal()['min'](3);` – 110precent Jul 15 '13 at 09:21
  • @110precent Even error message says that this is wrong syntax for php. Also, I hope you don't use stuff like this in real-world projects. 1. This really hurts eyes (and might hurt brain). 2. Syntax may change from version to version and the first thing that may become invalid is this kind of structure. – Leri Jul 15 '13 at 09:25

1 Answers1

3

PHP's parser is simply not up to the task, so you can't write this expression as you could have in other languages. You will have to use a workaround, for example:

call_user_func((new Obj())->check()['min'], 3));

Or alternatively:

$f = (new Obj())->check()['min'];
$f(3);
Jon
  • 428,835
  • 81
  • 738
  • 806