-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
PHP 7 Programming Cookbook
By :
An aspect that is critical to advanced PHP development is the use of namespaces. The arbitrarily defined namespace becomes a prefix to the class name, thereby avoiding the problem of accidental class duplication, and allowing you extraordinary freedom of development. Another benefit to the use of a namespace, assuming it matches the directory structure, is that it facilitates autoloading, as discussed in Chapter 1, Building a Foundation.
To define a class within a namespace, simply add the keyword namespace at the top of the code file:
namespace Application\Entity;
Best practice
As with the recommendation to have only one class per file, likewise you should have only one namespace per file.
The only PHP code that should precede the keyword namespace would be a comment and/or the keyword declare:
<?php
declare(strict_types=1);
namespace Application\Entity;
/**
* Address
*
*/
class Address
{
// some code
}In PHP 5, if you needed to access a class in an...