This article describes the simple usage of PHP namespace. Share it for your reference, as follows:
There are three files, namely, bar, in the same directory.
:
<?php namespace MyNamespace\Factory; class Employees{ private $name; function __construct($nameStr){ $this->name = $nameStr; } function getName(){ return 'Factory : '.$this->name; } }
:
<?php namespace MyNamespace\Company; class Employees{ private $name; function __construct($nameStr){ $this->name = $nameStr; } function getName(){ return 'Company : '.$this->name; } }
:
<?php //We all know that in the same directory, the same file of the same name cannot be stored. Then you can store files with the same name in different directories.//The full namespace is similar to the above meaning.//If there are two PHP files, there is an Employees class in the file. Creating two Employees objects in the same file is definitely not possible, and you can use the namespace at this time.$DIR = dirname(__FILE__); include($DIR.'/'); include($DIR.'/'); $obj = new MyNamespace\Factory\Employees('a'); $myName = $obj->getName(); echo "<p>$myName</p>"; $obj = new MyNamespace\Company\Employees('a'); $myName = $obj->getName(); echo "<p>$myName</p>";
Running results:
Factory : a
Company : a
For more information about PHP related content, please check out the topic of this site:PHP object-oriented programming tutorial》、《Introduction to PHP basic syntax》、《Summary of PHP operations and operator usage》、《Summary of PHP network programming skills》、《Complete collection of PHP array (Array) operation techniques》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope this article will be helpful to everyone's PHP programming.