in an effort to further my understanding of OOP, I've decided to refactor some of my code using an abstract class. The idea is roughly;
- One "parent" abstract class which forms a base for all child classs' to extend.
- One "helper" class which has a series of methods which children of the abstract class will need.
- the "helper" class will be used by other classes, so I don't want this to be an integral part of the abstract class.
The problem;
The child class extends the abstract class as intended, but PHP gives me a warning that the $helper argument is missing from the abstract class's constructor. I believe the constructor is being called because there isn't one in my child class, which is fine, but since you don't directly call an abstract class, how do I get this to work? Sample code below;
abstract class Parent_Abstract
{
public $input_helper_methods;
public function __construct( $helpers = NULL )
{
//set the helper methods
$this->input_helper_methods = $helpers;
}
}
The variable $helpers is in another file at the moment, which is included at the top of the file with the abstract class. Again, I think there's an issue with how this is being done. When I understand the structure I would like to use an autoloader, but for now, just manual would be good. This is the contents of that file;
class RD_Form_Input_Helper_Methods
{
private $var = 'something';
}
$helpers = new RD_Form_Input_Helper_Methods;
I hope this makes some sense. Thanks for taking the time to read/reply.
Another example;
//"helper" classes. I would like these methods to be available to Child_One and Child_Two
class Helper_Functions {}
class Formatting_Functions {}
abstract class Parent_Abstract()
{
private $helper_functions;
private $formatting_functions;
public function __construct( $object_one, object_two )
{
$this->helper_functions = $object_one;
$this->helper_functions = $object_two;
}
}
class Child_One extends Parent_Abstract
{
//I can use any of the properties or methods from the Helper_Functions or Formatting_Function class
}
class Child_Two extends Parent_Abstract
{
//I can use any of the properties or methods from the Helper_Functions or Formatting_Function class
}