Namespace Syntax in OOP in PHP

To assign a namespace to a class, you need to write the namespace command as the first line of the file in which this class is stored, and after it, separated by a space - the name of this namespace.

If a class belongs to some namespace, then to create an object of the class you will need to specify not only the class name, but also its namespace, separated by a backslash. Let's look at an example.

Suppose we have a Page class that does not belong to any namespace. Then we will create an object of this class as follows:

index.php<?php
  $page = new Page;
?>

Now suppose this class belongs to the Admin namespace. In this case, we will create an object of this class like this:

index.php<?php
  $page = new \Admin\Page;
?>

Let's separate the classes for users and classes for admins into different namespaces, to avoid the class conflicts described above.

For the Page class from the /admin/page.php file, let's specify the Admin namespace:

/admin/page.php<?php
  namespace Admin;
  
  class Page
  {
    
  }
?>

And for the Page class from the /users/page.php file, let's specify the Users namespace:

/users/page.php<?php
  namespace Users;
  
  class Page
  {
    
  }
?>

Now let's create an object of both Page classes in the /index.php file:

index.php<?php
  require_once '/admin/page.php';
  require_once '/users/page.php';
  
  $adminPage = new \Admin\Page;
  $usersPage = new \Users\Page;
?>
Задание 1

Suppose you have a core folder and a project folder. Each folder has its own Controller class. Make it so that these classes belong to different namespaces. In the index.php file, create objects of both classes.