PHP MVC from scratch - How to connect these classes?

First of all, I know that

  • not reinvent the wheel
  • There are many frameworks for MVC.

This is not for production work, but for my hobby. I write MVC from scratch to get a deeper understanding of MVC and to my own satisfaction. In the end, I can say to myself: “You made this buddy. You yourself wrote the MVC structure!”


To my problem now. Usually we will write some classes in MVC from the very beginning. We usually call them Bootstrapper , Router , Registry and Config . Maybe more, but now I would like to focus on them. And, as a rule, we do not need more than one instance of these classes for each request (singleton?). I think it’s very clear from the names themselves what these classes do. So, to my questions now:

Who starts over (I think this is Bootstrapper )? How are these classes related to each other? Should they all be single? How can we make instances of these ( Bootstrapper can be an exception), available for other classes in the application (maybe we use singleton)?

+4
source share
2 answers

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

  • Do not use Bootstrapper , Core or Initializer magic classes. It is much better to include this functionality in something like a bootstrap.php or init.php . In my project, such a file contains all the "postings" between other classes ( Router instances are created, different factories are inserted, etc.).

  • Do not use global state in your application (the most likely candidates for implementing global state will be Register and Config ). I would recommend you watch this playlist of this lecture to get some idea.

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

  • where the application starts depends on how you create the code, but it should not be kicked from inside the class. Think of other people (who will include you in 6 months) who might need to understand how your framework works. Digging through the thought of another class, just getting to the magic function init() will be annoying.

  • Find out what SOLID principles and Demeter Law is.

.. my two cents

+7
source

Your problem has little to do with MVC and is more about using global variables or global values.

Many of these classes usually require one instance for each class ("single"), and in some cases single games cannot be avoided, and in your case they are fine to have them.

How many books, textbooks, does not teach how, when and where singletones should be initialized. Its changes are from a programming language to a programming language.

And, in the case of websites where the variables lose their value when they go to another page, this becomes complicated.

There is this concept called "session variables" that work with single points, which allows you to save values ​​when moving from one page to another:

http://php.net/manual/en/session.examples.basic.php

Let you have a website. It has several files and several pages. Some of these php files are called directly and are considered "web pages", for example, "index.php".

Other php files are library files and are “required” or “included” by other files and are not considered “web pages” themselves.

When a user clicks a link on a wbe page and the browser calls up another web page, other values ​​may be lost. This is not like opening another form in a desktop application.

Think about it when a user first appears on a website (example: “index.php”): the main file “includes” or “requires” other files and initializes global variables or single packages:

 <?php // filename: "config.php" // another includes or requires here class Config { // begin data section public static $UserName; public static $UserPassword; // end data section // begin singleton section private static $_instance; public static function getInstance() { if (!self::$_instance instanceof self) { self::$_instance = new self; } return self::$_instance; } // end singleton section // start session section public static function SaveSession() { $_SESSION['config_username'] = Config::$UserName; $_SESSION['config_password'] = Config::$UserPassword; } public static function RestoreSession() { Config::$UserName = $_SESSION['config_username']; Config::$UserPassword = $_SESSION['config_password']; } // end session section } // class Config ?> <?php // filename: "index.php" include ("config.php"); // another includes or requires here class Application { public static function main() { // prepare session variables session_start(); // prepare singletons here $instance = Config::getInstance(); // this code its an example $instance->UserName = "johndoe"; $instance->UserPassword = "123"; $instance->SaveSession(); // all the page construction goes here // ... } } // // program starts here: Application::main(); ?> 

When the user navigates to another page, the application must reload the session data into singletones.

 <?php // filename: "customers.php" include ("config.php"); // another includes or requires here class Application { public static function main() { // copy data from session to singletons $instance->RestoreSession(); // all the page construction goes here // ... } } // // program starts here: Application::main(); ?> 

Odinal is not related, usually they are independent values.

Greetings.

+1
source

Source: https://habr.com/ru/post/1415344/


All Articles