D
D
dev4002016-05-19 21:53:04
PHP
dev400, 2016-05-19 21:53:04

Correct use of a constructor?

I write new objects to the properties, and use them in the heirs. Doesn't this violate OOP principles?

public function __construct()
    {
        $this->layout = new View('/layouts/single');
        $this->model = new Models\Catalog;
        $this->widgets = new Widgets;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dmitriy, 2016-05-19
@dev400

It's okay until you start testing, then you hit SOLID.

public function __construct(ViewInterface $View, CatalogInterface $Catalog,WidgetsInterface $Widgets)
    {
        $this->layout = $View;
        $this->model = $Catalog;
        $this->widgets = $Widgets;
    }

1. This code is better for testing
2. You depend on abstractions, not concrete objects

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question