Three ways to use php namespace

不言
Release: 2023-03-28 17:10:01
Original
1811 people have browsed it

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{
}
Copy after login

Reference method:


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

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);
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!