NotionCommotion Posted October 8, 2015 Share Posted October 8, 2015 (edited) 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 October 8, 2015 by NotionCommotion Quote Link to comment https://forums.phpfreaks.com/topic/298497-passing-anonymous-function-to-a-function/ Share on other sites More sharing options...
Jacques1 Posted October 8, 2015 Share Posted October 8, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/298497-passing-anonymous-function-to-a-function/#findComment-1522767 Share on other sites More sharing options...
NotionCommotion Posted October 8, 2015 Author Share Posted October 8, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/298497-passing-anonymous-function-to-a-function/#findComment-1522768 Share on other sites More sharing options...
Jacques1 Posted October 8, 2015 Share Posted October 8, 2015 Yes, I could store it as a temporary variable. I take it that is the "right" way to do it? Well, there aren't many options. Either you import the entire $a array, or you extract one particular element and import that. Quote Link to comment https://forums.phpfreaks.com/topic/298497-passing-anonymous-function-to-a-function/#findComment-1522771 Share on other sites More sharing options...
scootstah Posted October 8, 2015 Share Posted October 8, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/298497-passing-anonymous-function-to-a-function/#findComment-1522772 Share on other sites More sharing options...
NotionCommotion Posted October 8, 2015 Author Share Posted October 8, 2015 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'); } } } Quote Link to comment https://forums.phpfreaks.com/topic/298497-passing-anonymous-function-to-a-function/#findComment-1522773 Share on other sites More sharing options...
Solution Jacques1 Posted October 9, 2015 Solution Share Posted October 9, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/298497-passing-anonymous-function-to-a-function/#findComment-1522776 Share on other sites More sharing options...
scootstah Posted October 9, 2015 Share Posted October 9, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/298497-passing-anonymous-function-to-a-function/#findComment-1522781 Share on other sites More sharing options...
NotionCommotion Posted October 9, 2015 Author Share Posted October 9, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/298497-passing-anonymous-function-to-a-function/#findComment-1522807 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.