0

I am learning MVC in php. I saw following sample PHP MVC example

$model = new Model();
$controller = new Controller($model);
$view = new View($controller, $model); // $model already inside $controller!

If you notice the code, $controller object already contains $model.So the $view can access data $model via $controller.Then why we need to pass the $model object again along with $controller into $view? please help.

user5005768Himadree
  • 1,375
  • 3
  • 23
  • 61
  • 1
    It can be modified inside new Controller($model); Please read about SOLID, for example https://medium.com/prod-io/solid-principles-takeaways-ec0825a07247 – Vasyl Zhuryk Jul 11 '18 at 07:07
  • 1
    @VasylZhuryk if `$model` is modified inside controller, its modified everywhere, its the same object in and out of the controller, unless you create a clone inside the controller which is unlikely. – azjezz Jul 11 '18 at 07:11
  • 1
    That structure is wrong, because it has nothing to do with MVC. Where have you been getting this BS? – tereško Jul 11 '18 at 07:16
  • Thanks @tereško . I got this example from here [sitepoint](https://www.sitepoint.com/the-mvc-pattern-and-php-1/) confusing. – user5005768Himadree Jul 11 '18 at 08:56
  • 1
    @user5005768Himadree the main problem in that article comes from trying to simplify an architectural pattern, which is intended for large scale applications (20k+ lines of code as minimum). MVC is not intended for helloworld-size projects. They completely mis-represent the "model" part (that article keeps insisting that "model" is just a table abstraction, which is insanely stupid). Also, what the insist on calling "view" is basically a [template class](http://chadminick.com/articles/simple-php-template-engine.html). – tereško Jul 11 '18 at 10:08
  • imo, the `view` should be given all the data it needs to work with. It does not need to know anything about how the data is obtained that it uses. This simplifies testing. And also simplifies any modifications. – Ryan Vincent Jul 11 '18 at 10:08
  • 1
    @RyanVincent heh .. I happen to be of the completely opposite [opinion](https://stackoverflow.com/a/16596704/727208) :D – tereško Jul 11 '18 at 10:13
  • @tereško, yes, There is no 'one true way' to solve any problem! :) It all depends on the circumstances. and what you are trying to achieve. peace. – Ryan Vincent Jul 11 '18 at 10:16

1 Answers1

0

Example of code:

class UserModel
{
   public function getUserId()
   {
      return 100500;
   }

   public function getUserAddress()
   {
      return /*here is some data*/
   }
}

class Controller 
{
    private $userId;

    public function __construct(UserModel $model)
    {
        $this->userId = $model->getUserId();
    }
}

$controller = new Controller(UserModel);

As you see, we don't have model UserModel inside controller (only id). But, what if you Wiew need function getUserAddress()? You must put object inside

Vasyl Zhuryk
  • 1,228
  • 10
  • 23
  • Thanks. But the view by it self OR via controller can access same model and thus gives same output: `$m = new UserModel; $c = new Controller($m); $v= new View($m, $c); // following echo gives same output! echo $v->model->getUserAddress(); echo $v->controller->model->getUserAddress(); `So why to keep both ways to access model? – user5005768Himadree Jul 11 '18 at 09:18
  • 1
    @user5005768himadree That is not good solution. Good MVC is: create Model in controller and put it to view. In view you can access only to Model (but not to controller). – Vasyl Zhuryk Jul 12 '18 at 07:06