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!
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:
<?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.
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:
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
}
}
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.
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:
<?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
}
}
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.