Jump to content

Use callback function in other class


NotionCommotion

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.