Namespaces overview

(PHP 5 >= 5.3.0, PHP 7)
Examples:
Namespace syntax example

PHP Namespaces provide a way in which to group related classes, interfaces, functions and constants. Here is an example of namespace syntax in PHP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
namespace my\name; // see "Defining Namespaces" section
 
class MyClass {}
function myfunction() {}
const MYCONST = 1;
 
$a new MyClass;
$c new \my\name\MyClass; // see "Global Space" section
 
$a strlen('hi'); // see "Using namespaces: fallback to global
                   // function/constant" section
 
$d namespace\MYCONST; // see "namespace operator and __NAMESPACE__
                        // constant" section
$d = __NAMESPACE__ . '\MYCONST';
echo constant($d); // see "Namespaces and dynamic language features" section
?>
doc_php
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.