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.
namespace at the top of the code file:namespace Application\Entity;
namespace would be a comment and/or the keyword declare:<?php
declare(strict_types=1);
namespace Application\Entity;
/**
* Address
*
*/
class Address
{
// some code
}use statement containing only the namespace. You would need to then prefix any class reference within this namespace with the last component of the namespace:use Application\Entity; $name = new Entity\Name(); $addr = new Entity\Address(); $prof = new Entity\Profile();
use Application\Entity\Name; use Application\Entity\Address; use Application\Entity\Profile; $name = new Name(); $addr = new Address(); $prof = new Profile();
use Application\Entity\ {
Name,
Address,
Profile
};
$name = new Name();
$addr = new Address();
$prof = new Profile();function __autoload($class)
{
echo "Argument Passed to Autoloader = $class\n";
include __DIR__ . '/../' . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
}For illustration purposes, define a directory structure that matches the Application\* namespace. Create a base folder Application, and a sub-folder Entity. You can also include any sub-folders as desired, such as Database and Generic, used in other chapters:

Next, create three entity classes, each in their own file, under the Application/Entity folder: Name.php, Address.php, and Profile.php. We only show Application\Entity\Name here. Application\Entity\Address and Application\Entity\Profile will be the same, except that Address has an $address property, and Profile has a $profile property, each with an appropriate get and set method:
<?php
declare(strict_types=1);
namespace Application\Entity;
/**
* Name
*
*/
class Name
{
protected $name = '';
/**
* This method returns the current value of $name
*
* @return string $name
*/
public function getName() : string
{
return $this->name;
}
/**
* This method sets the value of $name
*
* @param string $name
* @return name $this
*/
public function setName(string $name)
{
$this->name = $name;
return $this;
}
}You can then either use the autoloader defined in Chapter 1, Building a Foundation, or use the simple autoloader mentioned previously. Place the commands to set up autoloading in a file, chap_04_oop_namespace_example_1.php. In this file, you can then specify a use statement which only references the namespace, not the class names. Create instances of the three entity classes Name, Address and Profile, by prefixing the class name with the last part of the namespace, Entity:
use Application\Entity; $name = new Entity\Name(); $addr = new Entity\Address(); $prof = new Entity\Profile(); var_dump($name); var_dump($addr); var_dump($prof);
Here is the output:

Next, use Save as to copy the file to a new one named chap_04_oop_namespace_example_2.php. Change the use statement to the following:
use Application\Entity\Name; use Application\Entity\Address; use Application\Entity\Profile;
You can now create class instances using only the class name:
$name = new Name(); $addr = new Address(); $prof = new Profile();
When you run this script, here is the output:

Finally, again run Save as and create a new file, chap_04_oop_namespace_example_3.php. You can now test the group use feature introduced in PHP 7:
use Application\Entity\ {
Name,
Address,
Profile
};
$name = new Name();
$addr = new Address();
$prof = new Profile();Again, when you run this block of code, the output will be the same as the preceding output:
