Use Command and Relative Paths in OOP in PHP
When using the use command, you can
specify relative paths.
Let's look at an example.
Suppose we are including a certain class:
<?php
namespace Core\Admin;
use \Core\Admin\Path\Router; // connect the class
class Controller extends Router
{
}
?>
As you can see, the beginning of the namespace of the connected class matches the current namespace. This means that we can remove this part when connecting our class, while also removing the initial backslash:
<?php
namespace Core\Admin;
use Path\Router; // make a relative path
class Controller extends Router
{
}
?>
Simplify the following code using
use:
<?php
namespace Core\Storage;
class Model
{
public function __construct()
{
$database = new \Core\Storage\DataBase;
}
}
?>