Jump to content

Type declarations in child as well as parent?


NotionCommotion

Recommended Posts

If calling a parents constructor, it is considered better practice to duplicate the type declarations or just use them in the parent?  Originally, I was doing for both, but it gets a bit cumbersome sometimes, and thinking of changing.  Thanks

 

namespace Base\Bla\BlaBla\Foo;
class FooClass
{
    protected $mapper, $validator, $pdo, $properties;

    public function __construct(
        Mapper\MapperClass $mapper,
        Validator\ValidatorClass $validator,
        \PDO $pdo,
        array $properties
    ) {
        $this->mapper=$mapper;
        $this->validator=$validator;
        $this->pdo=$pdo;
        $this->properties=$properties;
    }
}

Option 1

namespace Base\SpecificFoo;
class SpecificFooClassx extends \Base\Bla\BlaBla\Foo\FooClass
{
    protected $bar;

    public function __construct(
        Bar\BarClass $bar,
        $mapper,
        $validator,
        $pdo,
        $properties
    ) {
        $this->bar=$bar;
        parent::__construct($mapper, $validator, $pdo, $properties);
    }
}

Option 2

namespace Base\SpecificFoo;
class SpecificFooClass extends \Base\Bla\BlaBla\Foo\FooClass
{
    public function __construct(
        Bar\BarClass $bar,
        \Base\Bla\BlaBla\Foo\FooClass\Mapper\MapperClass $mapper,
        \Base\Bla\BlaBla\Foo\FooClass\Validator\ValidatorClass $validator,
        \PDO $pdo,
        array $properties
    ) {
        $this->bar=$bar;
        parent::__construct($mapper, $validator, $pdo, $properties);
    }
}

 

Link to comment
Share on other sites

Duplicate them. Leaving them off is such an incorrect decision to me that I don't even have a clever way to answer your question.

The types mean something. They aren't arbitrary. Writing those huge long class names is ridiculous, of course, but that's why PHP has the "use" keyword.

use Base\Bla\BlaBla\Foo as Foo;
class SpecificFooClass extends Foo\FooClass {

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.