First of all, I know that
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)?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With