Jump to content

Passing Anonymous Function to a function


NotionCommotion
Go to solution Solved by Jacques1,

Recommended Posts

Am I doing this correctly?  How would I do it if I wished to pass $a['x'] instead of $a, and wanted a normal variable within the callback to represent the passed $a['x']?


<?php
class foo
{
    public function bar()
    {
        $a=123;
        //$a['x']=123;
        $callback=function($b) use ($a){
            return ($a==$b);
        };
        $this->other($callback);
    }

    private function other($cb)
    {
        $B=123;
        if($cb($B) ) {
            echo('$a == $b');
        }
    }
}

$foo=new foo();
$foo->bar();
?>
Edited by NotionCommotion
Link to comment
Share on other sites

You seem to overuse closures a bit. ::)

 

Regarding your question: Can't you store $a['x'] in a temporary variable (like $x) and pass that to the closure?

 

I figure I should go crazy with them for a while, and then go back to reality!

 

Yes, I could store it as a temporary variable.  I take it that is the "right" way to do it?

Link to comment
Share on other sites

I figure I should go crazy with them for a while, and then go back to reality!

I would argue that you should instead learn the proper application of callbacks and anonymous functions. I don't mean that in a mean way, but you are using them in a weird and non-typical way. Frankly, what you're trying to do doesn't make sense.

Link to comment
Share on other sites

I would argue that you should instead learn the proper application of callbacks and anonymous functions. I don't mean that in a mean way, but you are using them in a weird and non-typical way. Frankly, what you're trying to do doesn't make sense.

 

 

I have two methods, bar1() and bar2() which do different things.  They both use method other() and use it 99% the same way and the only difference will be a conditional IF statement.

 

Instead of using an anonymous function, I "could" pass some value to other() and put the logic to use one of the two conditional checks there.

 

Would you recommend doing it that way, with an anonymous function, or some other way all together?

<?php
class foo
{
    public function bar1()
    {
        $a=123;
        $callback=function($b) use ($a){
            return ($a==$b);
        };
        $this->other($callback);
    }
    public function bar2()
    {
        $a=246;
        $callback=function($b) use ($a){
            return ($a==2*$b);
        };
        $this->other($callback);
    }

    private function other($cb)
    {
        $B=123;
        if($cb($B) ) {
            echo('$a == $b');
        }
    }
}
Link to comment
Share on other sites

  • Solution

It's difficult to give advice for this extremely abstract scenario.

 

Generally speaking, a method should make sense on its own and not have hidden dependencies all over the place. You should be able to give a clear description of what this particular method does. If you cannot do that, you should refactor it. Maybe you need an additional parameter, maybe you need to break the logic into multiple methods, maybe you have to do a lot more. That depends on the actual problem (which you haven't stated yet).

 

Callbacks do make sense in some special scenarios, but you shouldn't use them as a standard pattern. When you're starting to pile up closures and your requirements are becoming weirder and weirder, it's definitely time to stop.

Link to comment
Share on other sites

I have two methods, bar1() and bar2() which do different things.  They both use method other() and use it 99% the same way and the only difference will be a conditional IF statement.

 

Instead of using an anonymous function, I "could" pass some value to other() and put the logic to use one of the two conditional checks there.

 

Would you recommend doing it that way, with an anonymous function, or some other way all together?

<?php
class foo
{
    public function bar1()
    {
        $a=123;
        $callback=function($b) use ($a){
            return ($a==$b);
        };
        $this->other($callback);
    }
    public function bar2()
    {
        $a=246;
        $callback=function($b) use ($a){
            return ($a==2*$b);
        };
        $this->other($callback);
    }

    private function other($cb)
    {
        $B=123;
        if($cb($B) ) {
            echo('$a == $b');
        }
    }
}

 

I mean, why not just do the logic in the method itself?

 

Anonymous functions are used when you need to pass a callable, but don't want to create a function or class just for that one thing. They're like one-time use functions. They can also be used to isolate scope.

 

What you're doing here though doesn't really make sense. You have two separate methods that both define an anonymous function - why not just put the logic from the anonymous function into the class method?

 

It's hard to really see where you're trying to go or what you're trying to do with your code, so it's hard to suggest something different. What problem are you trying to solve? How is this code actually going to be used in a real-world scenario; what is it doing?

Link to comment
Share on other sites

Thanks again for your replies.  For my case, the logic belongs in bar1() and bar2(), and not other().  That being said and given your advice, I agree with both of you that I shouldn't be doing this way, and will move a small amount of script from other() to bar1() and bar2() thereby eliminating the need for a call back and making the script much more straightforward.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.