Defining namespaces

(PHP 5 >= 5.3.0, PHP 7)
Examples:
Declaring a single namespace

Although any valid PHP code can be contained within a namespace, only the following types of code are affected by namespaces: classes (including abstracts and traits), interfaces, functions and constants.

Namespaces are declared using the namespace keyword. A file containing a namespace must declare the namespace at the top of the file before any other code - with one exception: the declare keyword.

1
2
3
4
5
6
7
8
<?php
namespace MyProject;
 
const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */ }
 
?>
Declaring a single namespace
1
2
3
4
<html>
<?php
namespace MyProject; // fatal error - namespace must be the first statement in the script
?>
doc_php
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.