PHP Basic MVC Framework Part 4 - Loading Models

preview_player
Показать описание
Source Files:
Рекомендации по теме
Комментарии
Автор

@thephpecho Great reply! I kinda now see the benefit of using a loader. I'll just mention I'm from a Java background and I find that the best way to do dependency injection is to use polymorphism and abstraction. For example you could pass in a Model to the controller's constructor using the superclass for type hinting: __construct(baseModel $model) and instead of using getEntries() use a polymorphic method such as getItems() common to all baseModels...continued...

abnegatingconformity
Автор

@thephpecho part 2: I know that PHP has very little type safety but using this method does mean you could pass in any concrete implementation of baseModel you like. You could do something similar with views e.g. have a controller method that goes view(View $view) and pass in any View implementation. Does this sound like a sensible way to overcome the coupling and DI issue? It seems more OOP friendly too. Unless I'm still missing something. Thanks again.

abnegatingconformity
Автор

Great tutorial, but I have always wondered what advantage there is to loading models into a registry in this way. Is it so they can be accessed globally from anywhere that calls Registry::getInstance()? If so, why is this an advantage over simply loading your model with and then having a class var inside baseController such as protected $model; and simply going $this->model = new postsModel(); or something.

abnegatingconformity
Автор

You're going way to fast and arent clearly explaining what you are actually doing, further then that its a great tutorial for people that already know some php.

basvandriel
welcome to shbcf.ru