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.

4
  • 1
    The most valid way is (new Obj())->check()['min'](3); - what is your result ? Any error ? Commented Jul 15, 2013 at 9:16
  • @hsz: That would be valid and correct in a language with a formal grammar. PHP is not that language. Commented Jul 15, 2013 at 9:20
  • Parse error: syntax error, unexpected '(' Buy in real enviroment I use (new Valid())->checkVal()['min'](3); Commented Jul 15, 2013 at 9: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. Commented Jul 15, 2013 at 9:25

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

What about ((new Obj())->check()['min'])(3) ?
@hsz still invalid syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.