The Little Guy Posted July 29, 2011 Share Posted July 29, 2011 I can not thing of a reason why someone would use a callback function in php. does anyone have any good examples of when/why you might use a callback? Quote Link to comment https://forums.phpfreaks.com/topic/243220-callback-functions/ Share on other sites More sharing options...
AyKay47 Posted July 29, 2011 Share Posted July 29, 2011 in reality, all user-defined functions are callback functions..some defined functions allow for a function passed as an argument, and sometimes callback functions can be useful inside other functions... here are some references.. http://www.sitepoint.com/forums/php-application-design-147/example-callback-php-336758.html http://www.php.net/callback Quote Link to comment https://forums.phpfreaks.com/topic/243220-callback-functions/#findComment-1249153 Share on other sites More sharing options...
The Little Guy Posted July 29, 2011 Author Share Posted July 29, 2011 I am making a library, and I am not sure if some functions should have callbacks or not. I am not sure if it would be useful or not. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/243220-callback-functions/#findComment-1249156 Share on other sites More sharing options...
AyKay47 Posted July 29, 2011 Share Posted July 29, 2011 hard to say without seeing your logic for the library...it really depends how you have your code setup..callback functions can be very useful if you would like to pass an argument from another function to the calling function Quote Link to comment https://forums.phpfreaks.com/topic/243220-callback-functions/#findComment-1249161 Share on other sites More sharing options...
The Little Guy Posted July 29, 2011 Author Share Posted July 29, 2011 Here is how I was planing on implementing a callback: <?php class TheCoolClass{ public $working = ""; function do_something($callback){ $this->working = "I am doing something..."; return call_user_func($callback); } } $tcc = new TheCoolClass(); echo $tcc->do_something(function(){ return "hello"; }); echo $tcc->working; ?> Quote Link to comment https://forums.phpfreaks.com/topic/243220-callback-functions/#findComment-1249170 Share on other sites More sharing options...
AyKay47 Posted July 29, 2011 Share Posted July 29, 2011 that would work but I do not see the reason for a callback function.. Quote Link to comment https://forums.phpfreaks.com/topic/243220-callback-functions/#findComment-1249178 Share on other sites More sharing options...
The Little Guy Posted July 29, 2011 Author Share Posted July 29, 2011 that was just an example. The problem I am having, is I don't know when a user would wan't to use a callback function. I can not think of a good example why I should let them use a callback in my code. Quote Link to comment https://forums.phpfreaks.com/topic/243220-callback-functions/#findComment-1249181 Share on other sites More sharing options...
trq Posted July 29, 2011 Share Posted July 29, 2011 I can not think of a good example why I should let them use a callback in my code. Then you don't need them. Quote Link to comment https://forums.phpfreaks.com/topic/243220-callback-functions/#findComment-1249212 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.