PHP namespace and automatic loading principle and usage example analysis

Time:2020-11-26

This paper describes the PHP namespace and automatic loading principle and usage. For your reference, the details are as follows:

PHP namespace

After php5.3, the feature of namespace is introduced. In essence, a namespace is a container in which you can put classes, functions and variables. In a namespace, you can access these items unconditionally. Outside the namespace, you must import or reference a named space to access the items it contains.

Declaring Namespace

namespace my;
require_one 'outputter3.php';

class outputter {
  //Output data
  public function helloworld() {
    echo "hello world!";
  }

}

namespace useful;

class outputter {

}

namespaceKeyword is used to create a namespace, and when a namespace is generally declared,namespacePHP also supports declaring nested namespace, such as


namespace com\getinstance\util;

using namespace std

Within this namespace, you can directly call classes, functions and variables in the namespace,

Because I’m already in the namespace, I don’t have to prefix the class name with any kind of path,

If you plan to access a class from outside the namespace environment, you can use:


com\getinstance\util\Debug::helloworld();

The following code will report an error:


namespace main;

com\getinstance\util\Debug::helloworld();

Because of the relative namespace used, PHP looks in the main namespacecom\getinstance\util, but not found. Just like when creating absolute URLs and file paths, you can also use this method to create absolute namespace, as follows:


namespace main;

\com\getinstance\util\Debug::helloworld();

The leading backslash tells PHP to search from the root namespace instead of the current namespace.

However, if it is unreasonable to write such a long prefix every time the namespace is called, PHP can use ituseKeywords are used to alias other namespace in the current namespace, as follows:


namespace main;
use com\getinstance\util;

util\Debug::helloworld();

Importcom\getinstance\util, and implicitly uses an alias for itutil

If you don’t want to introduce a namespace, you can import the debug class itself:


namespace main;
use com\getinstance\util\Debug;

util\Debug::helloworld();

keyworduseUse afteras, you can aliasDebugRevised asuDebug


namespace main;
use com\getinstance\util\Debug as uDebug;

uDebug::helloworld();

To access a global space (non namespace) in a namespace, you can add a backslash before the global class

namespace com\getinstance\util;
require "global.php";

\Lister:: helloworld(); // access global space
Lister:: helloworld(); // access the local namespace

use__NAMESPACE__ Constants can output the current namespace and are useful for debugging.

Auto load

PHP5 introduces__autoload()Method to automatically include class files. When the PHP engine encounters an operation that attempts to instantiate an unknown class, it will call__autoload()Method, if defined, and pass the class name to it as a string parameter, and write__autoload()A policy should be defined to locate and include missing class files. As follows:


function __autoload($classname) {
  include_once("$classname.php");
}
$product = new ShopProduct();

More about PHP related content interested readers can see this site topic: “PHP object-oriented programming introductory tutorial”, “PHP basic syntax tutorial”, “PHP operation and operator Usage Summary”, “PHP network programming skills summary”, “PHP array (array) operation skills”, “PHP string (string) Usage Summary”, “PHP + MySQL database operation introduction” “Tutorial” and “PHP common database operation skills summary”

I hope this article will help you with PHP programming.