Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Pass Instance Of Object To Abstract Class Constructor

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
}
like image 896
Dan Avatar asked Jan 23 '26 08:01

Dan


1 Answers

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.

You would only get such a warning if the abstract class constructor would require the $helper parameter. But your code is different, the $helper parameter is optional:

abstract class Parent_Abstract
{
    public $input_helper_methods;

    public function __construct( $helpers = NULL )
    {                            ###############      $helpers is optional

Now as I quote it, it also has a different name $helper =/= $helpers.

So most likely the code example you give in your question is incomplete and with errors.

Let's just outline a new one modeled after your question:

abstract class AbstractBaseClass
{
    private $helper;

    function __construct($helper)
    {
        $this->helper = $helper;
    }
}

class Concrete extends AbstractBaseClass
{
}

The differences in short: the $helper field (or property) is private and not public; the $helper ctor parameter is required.

Using that code and instantiating a concrete childclass is giving your said error:

// Warning: Missing argument 1 for AbstractBaseClass::__construct()

$obj = new Concrete();

That is because the helper is missing. To make this very simple, let's assume the helper is actually a secret number you need in complex calculations all done by such concrete classes. The number is 42:

$obj = new Concrete(42);

Now the error is gone. And the helper field is properly initialized:

print_r($obj);

Concrete Object
(
    [helper:AbstractBaseClass:private] => 42
)

As you can see, the private field of the abstract class has been set. The helper (here the number 42) has been assigned to it.

like image 141
hakre Avatar answered Jan 25 '26 20:01

hakre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!