Home >Backend Development >PHP Tutorial >Three ways to use php namespace
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!