Home  >  Article  >  Backend Development  >  Three ways to use php namespace

Three ways to use php namespace

不言
不言Original
2018-05-31 14:27:091779browse

The namespace in PHP 5.3 is actually a good thing, which can simplify programming. Here are three types of methods to access classes in the namespace in the code
1 Reference namespace and classes
Assume that the namespace program is namespaced-class.php

namespace Christmas\DaysOf;  
class PartridgeInAPearTree{
}

Reference method:


  include 'namespaced-class.php';
$bird1 = new Christmas\DaysOf\PartridgeInAPearTree();
var_dump($bird1);

At this time, the complete namespace and the following classes are introduced during NEW

2 Partial reference

  include 'namespaced-class.php';
use Christmas\DaysOf;
$bird2 = new DaysOf\PartridgeInAPearTree();
var_dump($bird2);

After USE specifies the namespace, when using it, you only need to quote the last part of the namespace, Daysof.

3 The simplest

 include 'namespaced-class.php';
use Christmas\DaysOf\PartridgeInAPearTree as Bird;            
$bird3 = new Bird();
var_dump($bird3);

Here, the specified classes under the namespace are replaced with a custom name, which is very convenient.

Related recommendations:

Detailed explanation of the steps to staticize the PHP page

The above is the detailed content of Three ways to use php namespace. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn