1

This a little hard for me to explain, but I'll try my best. I'm trying to find the best way to create an instance of a closure. Below is an example of how I am creating and accessing the closure:

$myvar->parse('var1, var2', function () { //my code }); 

I then loop through the strings by exploding the comma and put it into an array like so.

$array = array(); $array['var1'] = closure(); $array['var2'] = closure(); 

Later on in my code I use call_user_func to execute the function.

Now, the issue I'm having is that when I access $array['var1'] it calls the closure without any problem.

But when I access $array['var2'] it does nothing. I've been looking at diverse websites for a while without much luck. Do I need to use pointers (tried without success) or create a new instance of it somehow?

I currently only have access to php 5.3 on my server, so I can't use any of the awesome stuff in 5.4 :(

I'd really appreciate any feedback and advice you may have.

4
  • 1
    What the heck is "an instance of a closure"? Commented Jun 26, 2012 at 22:56
  • As others have noticed in answers, your explanation somewhat fails. The most visual gap I can see is the one between the first code example you have posted and the closure() expression - they are totally unrelated. So you could for example tell us what var_dump(closure()); outputs. Commented Jun 27, 2012 at 0:58
  • @Niko: In PHP, the instance of a closure is an instance of the object of type Closure that can be passed along and assigned to variables or parameters of function calls. Originally, the implementors of the PHP language details in version 5.3 wanted to hide the fact that a closure is in fact a new type of object named Closure, but as PHP was a language related to the internet, and the internet does not allow much of a feature to be hiddern behind a certain feature, it naturally came to public awareness that the "new" callable type in 5.3 is an object of type Closure which has instances. Commented Jun 27, 2012 at 1:06
  • Apologies, for not explaining better.. it was late where I am and was a bit difficult to elaborate. The user @Wrikken bellow kind of understood what I was trying to achieve.. although its still not right. Thanks for taking the time to look into it. Commented Jun 27, 2012 at 6:13

2 Answers 2

1
 $yourcall = function () { //code }; $array['var1'] = $yourcall; $array['var2'] = $yourcall; 

Would assign the closure to those values. However:

 $array['var1'] = $yourcall(); $array['var2'] = $yourcall(); 

As you seem to have would assign the result of calling that closure to the array items.

In your parse function (why does it take a string and not an array), I assume you want this:

 function parse($string,$closure){ $array = array(); foreach(explode(',',$string) as $key) $array[$key] = $closure; return $array; } 
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for taking a look into my problem.. you explained it better than i did.. I'll try out what you posted :)
Pfah, I made a better suggestion: actually feed it an array, separate the concerns :P
0

Yes your question isn't clear. You knew it so I wonder why you didn't explained better.

Anyway if you need to run a closure stored inside $array['var2'] you have to specifically put in your code something like this:

$array['var2'] = function(){ //> Code }; 

That's the only way

By guessing at your code, your function parse should like:

function parse($keys,$func) { $array = array(); foreach(explode(',',$keys) as $v) { $array[trim($v)] = $func; //> trim important here! } } 

Most likely you have forgot to trim($v)

2 Comments

Apologies for not explaining better, it was late where I am when I posted and I was a bit tired. My bad. The use @Wrikken (above) - Explained my problem a little better. I'll try what both of you have posted. Thanks.
you were right. I had forgot to use trim! My bad.. Thanks for helping

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.