Page 1 of 1

callback function from parent to child

Posted: Thu Aug 12, 2010 3:59 am
by zensys
I have a general class with several specialised subclasses. The general class needs to execute a function with specialised code from the child class before it can continue. I do not know how to store the function in the child class to pass it to the constructor of the parent. I cannot use the name of the child class because there are several. Here is a simplified example:

Code: Select all

<?php
class General
{
	public function __construct($callback)
	{
		$callback(); // or call_user_func($callback);
		// general code
	}
}	

class Special extends General
{
	private function _callback()
	{
		// specialised code
	}
	
	public function __construct()
	{
		$callback = $this->_callback();
		parent::__construct($callback);
		// other specialised code
	}	
}
I know there are other ways to do this but I just wanted to know if the callback solution could work. That way the parent class can do everything from the contructor.

Re: callback function from parent to child

Posted: Thu Aug 12, 2010 9:02 am
by AbraCadaver
First off you can't call a private function in Special from General. It will need to be protected or public. Second, if you will know the name of the callback, then it's simple:

Code: Select all

class General
{
        public function __construct()
        {
                $this->_callback();
                // general code
        }
}      

class Special extends General
{
        protected function _callback()
        {
                // specialised code
        }
       
        public function __construct()
        {
                parent::__construct();
                // other specialised code
        }      
}
If not, then something like this:

Code: Select all

class General
{
        public function __construct($callback)
        {
                $this->$callback();
                // general code
        }
}      

class Special extends General
{
        protected function _callback()
        {
                // specialised code
        }
       
        public function __construct()
        {
                parent::__construct('_callback');
                // other specialised code
        }      
}

Re: callback function from parent to child

Posted: Thu Aug 12, 2010 12:40 pm
by zensys
Thanks AbraCadaver, looks simple. After some googling I came to another solution. My parent class could not be used directly so I made it an abstract class with callback as an abstract protected method to be defined in the specialised children:

Code: Select all

<?php
abstract class General
{
	public function __construct($options)
	{
	    //general code A needed in B
		$this->_callback();
		// general code 
	}
	
	abstract protected function _callback();
}	

class Special extends General
{
	public function __construct()
	{
		// specialised 
		parent::__construct($options);
		
	}
	
    protected function _callback();
    	{
    		// specialised code B dependent on A
    	}
}
What would be your preference?

Re: callback function from parent to child

Posted: Thu Aug 12, 2010 2:32 pm
by AbraCadaver
I don't have any formal OOP training, but that's actually probably the preferred way. Good job.