Understand Php Autoload

Let us do little research on the loader mechanism of PHP project. Loader mechanism is the method to load these PHP files the main program need to use

how these old PHP projects load base classes and functional classes.

  1. Opencart 1.5.6.3

    In the index.php of opencart 1.5.6.3, we can find the following statement near the top of few lines

    require_once(DIR_SYSTEM . ‘startup.php’);

    DIR_SYSTEM is a constant which defined in config.php, and the value the path of system folder on server

    The following code is part of the file upload/system/startup.php

// Helper
require_once(DIR_SYSTEM . 'helper/json.php'); 
require_once(DIR_SYSTEM . 'helper/utf8.php'); 

// Engine
require_once(DIR_SYSTEM . 'engine/action.php'); 
require_once(DIR_SYSTEM . 'engine/controller.php');
require_once(DIR_SYSTEM . 'engine/front.php');
require_once(DIR_SYSTEM . 'engine/loader.php'); 
require_once(DIR_SYSTEM . 'engine/model.php');
require_once(DIR_SYSTEM . 'engine/registry.php');

// Common
require_once(DIR_SYSTEM . 'library/cache.php');
require_once(DIR_SYSTEM . 'library/url.php');
require_once(DIR_SYSTEM . 'library/config.php');
require_once(DIR_SYSTEM . 'library/db.php');
require_once(DIR_SYSTEM . 'library/document.php');
require_once(DIR_SYSTEM . 'library/encryption.php');
require_once(DIR_SYSTEM . 'library/image.php');
require_once(DIR_SYSTEM . 'library/language.php');
require_once(DIR_SYSTEM . 'library/log.php');
require_once(DIR_SYSTEM . 'library/mail.php');
require_once(DIR_SYSTEM . 'library/pagination.php');
require_once(DIR_SYSTEM . 'library/request.php');
require_once(DIR_SYSTEM . 'library/response.php');
require_once(DIR_SYSTEM . 'library/session.php');
require_once(DIR_SYSTEM . 'library/template.php');
require_once(DIR_SYSTEM . 'library/openbay.php');
require_once(DIR_SYSTEM . 'library/ebay.php');
require_once(DIR_SYSTEM . 'library/amazon.php');
require_once(DIR_SYSTEM . 'library/amazonus.php');

There are a lots of require_once statement to include the base class and module class there. What we should do if we want to add a library class? First, we should write that file under system\library folder, then we add another requirement_once statement in upload/system/startup.php.

  1. Wordpress

    Wordpress is not object oriented system. I haven’t plan to analysis the implementation of this system deeply. We only focus on the loader part.

    after open the wp-settings.php file under the root folder of Wordpress, we can see the code show shows below.

    App Store

    The wordpress have lots of require statement, and the require statement distributed very diverse.

    Secondly, are there better way to load file when system startup?
    Answer is yes. PHP 5.3+ provides a SPL function to register customized loader function. After we registered the loader function, PHP will call the function to load file when php notice there is a class haven’t loaded.

    Index.php
    <?php

    function autoloader($class) {
      include 'lib/' . $class . '.php';
    }
    
    spl_autoload_register('autoloader');
    
    $a = new A();
    $out = $a->plus(1,2);
    echo $out;
    
    lib/A.php
    

    <?php

    class A{

    public function plus($a, $b){
      return $a + $b;
    }
    

    }

    App Store

    We will get 3 after executed this program. The autoloader function automatically called to load class A. The load rule depends on how we want to locate the file path.

    Finally, all the new PHP projects use this way to load php file on startup, such as, YII2, Laravel, and Drupal 8.