callback function from parent to child

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zensys
Forum Newbie
Posts: 9
Joined: Mon May 31, 2010 5:16 am

callback function from parent to child

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: callback function from parent to child

Post 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
        }      
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
zensys
Forum Newbie
Posts: 9
Joined: Mon May 31, 2010 5:16 am

Re: callback function from parent to child

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: callback function from parent to child

Post by AbraCadaver »

I don't have any formal OOP training, but that's actually probably the preferred way. Good job.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply