PHP builds its own MVC framework from scratch and other automatic loading methods are explained in detail.

Time:2019-9-9

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

The entry file of MVC framework was introduced before. Next, we want to complete a function of “automatically loading classes”. We put this function into practice.ImoocIn this basic class.

core\imooc.php:

<?php
namespace core;
class Imooc
{
  public static $classMap = array();
  static public function run()
  {
    p('ok');
    $route = new \core\route();
  }
  /*
  Automatic Loading Function
  */
  static public function load($class)
  {
    // Autoloading Class Library
    // new \core\Route()
    // $class = '\core\Route'
    // IMOOC.'/core/route.php'
    if(isset($classMap[$class])){
      return true;
    }else{
      $class = str_replace('\', '/', $class);
      $file = IMOOC.'/'.$class.'.php';
      if (is_file($file)) {
        include $file;
        self::$classMap[$class] = $class;
      }else{
        return false;
      }
    }
  }
}

In the code above,load()The main function of the method is to automatically load the class library.

The working principle of automatic loading:

When we arenewWhen a class does not exist, it triggersspl_autoload_registerRegister the method, and then use this method to introduce the class to be instantiated


spl_autoload_register('\core\Imooc::load');

We are in the entry file.index.phpRegistered:

<?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');
}else{
  ini_set('display_error','Off');
}
// Loading function library
include CORE.'/common/function.php';
// Loading Framework Core Files
include CORE.'/imooc.php';
// Registration Autoloading
// (When we new a non-existent class, it triggers core Imooc:: load)
spl_autoload_register('\core\Imooc::load');
\core\Imooc::run();

So, we arerunMethod instantiationrouteClass is not introduced manually


static public function run()
{
    p('ok');
    $route = new \core\route();
}

The above code,new \core\route()Triggeredload()Method, and then introduce the required files.

route.phpThe code is as follows:


<?php
namespace core;
class Route
{
  public function __construct(){
    p('route ok');
  }
}

Now let’s access the entry fileindex.phpIt callsImooc::runMethod, the browser is expected to output:

ok
route ok

So far, the project structure is as follows:

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.