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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.