0

I want to store a function in an associative array but PHP throws this error:

Parse error: syntax error, unexpected 'function' (T_FUNCTION) in C:\Program Files\wamp\www.... on line 12

Code is here: You may also get idea what I'm trying to do.

private $rules = array(
    "required" => array(
        "message" => "This field is required",
        "rule" => function($value) {
            return isset($value) && !empty($value);
        }
    )
);

There is no answer there in those questions mentioned by moderator but only is information. I have edited the question and it fully qualifies for an answer now. The comments which had solution to this problem was deleted by moderator although some of them needed to be.

  • There are a lot of answers in the question marked as duplicate. I believe you should *clearly* state *why* these answers don't fit your issue. Missing this information, I can see no apparent reason why this question should be reopened. – Uli Köhler Aug 02 '14 at 14:06
  • @UliKöhler I have no problem whether this question remains closed or reopens. The questions mentioned by the moderator does not answer the above asked question, the `how to` part. I don't have time to search through web and prove there is no answer on the web. What I'm saying is, that 2 questions of SO doesn't answer the `how to` part. – Muhammed Talha Akbar Aug 02 '14 at 15:22
  • I agree that it is impossible to prove something does not exist. However, I still don't get why you're asking for the `how to`, given that the answers to the duplicate question clearly states that it is is not possible (at least until PHP 5.3). Please state *clearly* what *exactly* is missing in the duplicate question. Maybe I don't understand `but only is information`, but I believe most others also don't. – Uli Köhler Aug 02 '14 at 17:33
  • @UliKöhler Nothing is impossible. I found a solution for this. Just initialize a local variable in `__construct` with array of anonymous functions and then assign it to object's property. That's simple and you're saying it is impossible. Duplicate questions do not contain solution. – Muhammed Talha Akbar Aug 02 '14 at 17:49
  • No, I'm not. I was saying that according to the duplicated question (i.e. its answers), it seems to be impossible till PHP 5.3 *without doing it differently*. To me it seems that your solution proposes doing it differently (nothing inherently wrong with that). If you already know the answer (which is a good thing!) or tried something, I can't see why you ask the question in the first place (or, if you discovered that later, why you don't edit the question and add what you tried and what part of the problem is still left). Maybe you could also add your concept as answer to the other question? – Uli Köhler Aug 02 '14 at 18:19
  • @UliKöhler No, I didn't know the answer already. When Gordon told that properties can't store anonymous functions then I used this workaround. Well, it was invented by me though. You know when someone asks, `How to use display:inline-block in older IEs`. The right answer would be showing the exact solution or a workaround for the problem. In the duplicate questions, text from manual is copied & pasted telling it can't be done. I'll not add an answer anywhere because when SO doesn't care to have a solution or workaround for a problem then I shouldn't too. – Muhammed Talha Akbar Aug 03 '14 at 05:30
  • What makes you think there exists a solution for your quesiton that is not a workaround? I believe for questions like `How to use display:inline-block in older IEs` there is in fact a solution.. SO does care, but SO is no wizard that can create a solution using magic if there is none. Judging from the answers to this and the duplicate question I see evidence that there might not be any answer but a workaround. – Uli Köhler Aug 03 '14 at 13:28

1 Answers1

0

As much as I'm informed, no. But you can stored them in a variable.

http://php.net/manual/en/functions.anonymous.php

Charlotte Dunois
  • 4,638
  • 2
  • 20
  • 39