PHP builds its own MVC framework from scratch


This paper illustrates how PHP builds its own MVC framework entry file from scratch. Share for your reference, as follows:

First, let’s look at the running process of the framework.

Entry File – > Definition Constant – > Introduction Library – > Automatic Loading Class – > Startup Framework – > Route Resolution – > Loading Controller – > Return Results

Entry file index.php:

Entry file
1. Define constants
2. Loading function library
3. Launching Framework
// Define the root directory of the current framework
define('IMOOC', __DIR__);
// Define the directory where the framework core file is located
define('CORE', IMOOC.'/core');
// The directory where the project files are located
define('APP', IMOOC.'/app');
// Define project debugging patterns
define('DEBUG', true);
// Determine whether the project is in debugging state
if (DEBUG) {
  // Set the error level: Display all errors
  ini_set('display_error', 'On');
// Loading function library
include CORE.'/common/function.php';
// Loading Framework Core Files
include CORE.'/imooc.php';

Common function. PHP in the framework core directory:

Output corresponding variables or arrays
function p($var){
  }elseif (is_null($var)) {
    echo '<pre style="position:relative;z-index:1000;padding:10px;border-radius:5px;background:#f5f5f5;border:1px solid #aaa;font-size:14px;line-height:18px;opacity:0.9;">'.print_r($var,true).'</pre>';

Framework core file imooc.php:

namespace core;
class Imooc
  static public function run()

Run the project, access the entry file index. php, browser output one as scheduled:

More readers interested in PHP-related content can see the topics of this site: Introduction to Php Object-Oriented Programming, Introduction to PHP Array Operating Skills, Introduction to PHP Basic Grammar, Summary of PHP Operating and Operator Usage, Summary of Php String Usage, and php+mysql Data. Introduction to Library Operations and Summary of Common Database Operating Skills in PHP

I hope this article will be helpful to everyone’s PHP program design.

Recommended Today

Java atomicinteger class using

A counter For ordinary variables, when multithreading operations are involved, classic thread safety problems will be encountered. Consider the following code: private static final int TEST_THREAD_COUNT = 100; private static int counter = 0; public static void main(String[] args) { final CountDownLatch latch = new CountDownLatch(TEST_THREAD_COUNT); Thread[] threads = new Thread[TEST_THREAD_COUNT]; for (int i = […]