HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

PHP Namespaces

In PHP, namespaces provide a way to organize code into logical groups, preventing naming conflicts between classes, interfaces, functions, and constants.

Namespaces are especially useful in large projects where multiple developers are working on different parts of the codebase or when integrating third-party libraries.

Syntax

<?php
namespace Html;
?>

Output

Using Namespaces

Any code that follows a namespace declaration is operating inside the namespace, so classes that belong to the namespace can be instantiated without any qualifiers. To access classes from outside a namespace, the class needs to have the namespace attached to it.

Example

<?php

$table = new Html\Table();
$row = new Html\Row();

?>

Output