NotionCommotion Posted October 3, 2014 Share Posted October 3, 2014 Is it possible to pass a callback function to other class's method? Is what I am attempting to do a bad idea? Thanks <?php class validate { public function __construct($data,$callback) { //A bunch of script goes here, and I don't want to duplicate it if($callback) { //Use $callback function to modify $data } } } class controller { public function savePage() { //A bunch of script goes here, and I don't want to duplicate it $validate=new validate(array('hello'),$this->callback); } } class controller_page1 extends controller { public function callback() { //script which will be used to modify future $data } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/291412-use-callback-function-in-other-class/ Share on other sites More sharing options...
Barand Posted October 3, 2014 Share Posted October 3, 2014 You need to define the class and method names in an array, and not just the callback function name. eg $cleansed = array_map(array('mysqli', 'real_escape_string'), $_POST)); Quote Link to comment https://forums.phpfreaks.com/topic/291412-use-callback-function-in-other-class/#findComment-1492612 Share on other sites More sharing options...
requinix Posted October 3, 2014 Share Posted October 3, 2014 Even cooler would be to use interfaces: you can pass the entire object as an argument and the receiving code knows that it definitely has a specific method on it that can be called. If that makes sense in your situation - I'm not sure if it does since there's not much code, and the design is a bit weird. Quote Link to comment https://forums.phpfreaks.com/topic/291412-use-callback-function-in-other-class/#findComment-1492622 Share on other sites More sharing options...
NotionCommotion Posted October 3, 2014 Author Share Posted October 3, 2014 I do need to learn about interfaces. I agree the design is a bit weird, and I have since rethought things. Quote Link to comment https://forums.phpfreaks.com/topic/291412-use-callback-function-in-other-class/#findComment-1492641 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.