Namespaces in PHP: one short name, many classes
| Introduction | |
| Two classes, one name | |
| Declaring a namespace | |
| Selecting with use | |
| Two at once need aliases | |
| On this site: three RelatedArticles |
Introduction
Every section of this site has a class called RelatedArticles — the OOP section, the versions section, the require section. Same short name, different article lists. Namespaces are the mechanism that makes this legal: the full name carries the section, and use picks which full name the short one means in the current file.
Two classes, one name
Without namespaces, loading two files that both declare class RelatedArticles ends in a fatal error: Cannot declare class RelatedArticles, because the name is already in use. One global flat list of class names does not scale past a single script — hence one list per namespace.
Declaring a namespace
The namespace statement is the first statement of the file (after a blank line under <?php). Everything declared below it gets the prefix as part of its full name. Two files may each declare their own Invoice as long as the prefixes differ — put both variants in one file and every block needs braces:
<?php
namespace Aredel\Demo\Billing {
class Invoice
{
}
}
namespace Aredel\Demo\Shipping {
class Invoice
{
}
}
Full names: Aredel\Demo\Billing\Invoice and Aredel\Demo\Shipping\Invoice — no collision.
Selecting with use
The full name is precise but long, so each file imports the one it needs with use and keeps calling it by the short name. Three separate jobs, all required on this site (there is no autoloader): require_once loads the file, use binds the short name, new creates the object:
<?php
namespace Aredel\Demo;
use Aredel\Dev\Php\Oop\RelatedArticles;
require_once $_SERVER['DOCUMENT_ROOT'] . '/dev/php/oop/RelatedArticles.php';
$related = new RelatedArticles();
$articles = $related->articles();
Drop the require_once and the class does not exist; drop the use and new RelatedArticles() looks for a global class that was never declared.
Two at once need aliases
One file sometimes needs two same-named classes at once. A second plain use of the same short name is its own fatal (Cannot use ... as RelatedArticles because the name is already in use) — rename on import with as:
<?php
namespace Aredel\Demo;
use Aredel\Dev\Php\Oop\RelatedArticles as OopArticles;
use Aredel\Dev\Php\Versions\RelatedArticles as VersionsArticles;
$oop = new OopArticles();
$versions = new VersionsArticles();
On this site: three RelatedArticles
The pattern from every article page on this site, extended to three sections at once. Line by line: the file joins the demo namespace, imports three same-named classes under three aliases, loads the three files (aliases do not load anything by themselves), then instantiates each list under its alias:
<?php
namespace Aredel\Demo;
use Aredel\Dev\Php\Oop\RelatedArticles as OopArticles;
use Aredel\Dev\Php\Versions\RelatedArticles as VersionsArticles;
use Aredel\Dev\Php\Require\RelatedArticles as RequireArticles;
require_once $_SERVER['DOCUMENT_ROOT'] . '/dev/php/oop/RelatedArticles.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/dev/php/versions/RelatedArticles.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/dev/php/require/RelatedArticles.php';
$oop = new OopArticles();
$versions = new VersionsArticles();
$require = new RequireArticles();
Three short names, three article lists, zero collisions — that is
the entire point of namespaces.
Next: OOP in PHP, where these classes
come from.
Article author: Arthur Isaev
| Development with PHP | |
| Namespaces in PHP | |
| PHP 8 Classes | |
| Call function from another file |