A
A
Agent Gus2017-07-06 14:50:31
Laravel
Agent Gus, 2017-07-06 14:50:31

Laravel. I do not understand the logic of the code?

I am learning from the lessons "Laravel Framework from Zero to Pro" . In principle, everything is clear, except for one code...
IndexController

class IndexController extends SiteController {
    public function __construct() {
        $menu = new \Corp\Repositories\MenusRepository(new \Corp\Menu);
        parent::__construct($menu);
        $this->bar = 'right';
        $this->template = env('THEME').'.index';
    }
}

MenusRepository
<?php
namespace Corp\Repositories;

use Corp\Menu;

class MenusRepository extends Repository {
  public function __construct(Menu $menu) {
    $this->model = $menu;
  }
}

?>

So...
I don't understand the functionality of this code.
parent::__construct(new \Corp\Repositories\MenusRepository(new \Corp\Menu));

I know it's a parent class constructor call.
Question about Laravel.
$menu = new \Corp\Repositories\MenusRepository(new \Corp\Menu); For what?
The result is:
public function __construct(Menu new \Corp\Repositories\MenusRepository(new \Corp\Menu)) 
{
  $this->model = $menu;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Igoshin, 2017-07-06
@yanminibaev

What nonsense are you being taught. Better read the doc and watch the laracasts ( https://laracasts.com). No one in the lair does that.
The env function cannot be used in code at all. All environment variables must be transferred to configs and it is desirable to specify default configs.
But in fact, if you do not understand the logic itself, then learn OOP and SOLID

V
Vyacheslav Plisko, 2017-07-06
@AmdY

The author of this code does not own php or laravel at the proper level. Better read the original documentation and delete the laracasts. And your question is related to php.
This code calls the constructor of the parent class, while the interface of the inheritor constructor differs from the parent, which violates the principles of SOLID.
php.net/manual/en/language.oop5.decon.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question