Jump to content

Recommended Posts

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
    }
}
?>
Link to comment
https://forums.phpfreaks.com/topic/291412-use-callback-function-in-other-class/
Share on other sites

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.

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.