Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MVC from the scratch - How to connect these classes?

First of all, I know that

  • don't reinvent the wheel
  • there are lot of frameworks out there for MVC.

This is not for a production work, but for my hobby project. I am writing an MVC from the scratch is to get more deeper idea about MVC, and to my own satisfaction. In end, I can say to myself: "You did it buddy. You have wrote an MVC framework by yourself!"


To my problem now. Usually, we will write some classes in MVC, and are there from the start. We usually call them, something like, Bootstrapper, Router, Registry, and Config. There may be more, but I would like to focus on these now. And normally, we don't need more than one instances of these classes per request(singleton?). I think it is very clear from the names itself what these classes do. So to my questions now:

Who starts first(I think it is Bootstrapper)? How these classes linked together? Are they all need to be singleton? How do we make the instances of these(Bootstrapper may be an exception) available to other classes in the application(maybe we use singleton)?

like image 581
Jomoos Avatar asked Feb 20 '23 07:02

Jomoos


1 Answers

Since I am doing the same exact thing now, here is my perspective:

  • do not use magical Bootstrapper, Core or Initializer classes. It is much better to put that functionality in something like a bootstrap.php or init.php file. In my project such file contains all the "wiring" between other classes (the instances of Router are created, different factories are injected and so on).

  • do not use global state in your application (the Register and Config would be the most likely candidates of introducing global state). I would recommend for you to watch this lecture playlist to gain some insight.

  • if answer is "singleton", you are asking the wrong question

  • where application starts, depends on how you create the code, but it should not be kicked of from inside the class. Think of other people people (that would include you, after 6 month) who might need to understand how your framework works. Digging thought another class, just to get to the magical init() function would be annoying.

  • learn what SOLID principles and Law of Demeter is.

.. my two cents

like image 147
tereško Avatar answered Feb 22 '23 22:02

tereško