Namespace in PHP. Code example open

Namespace in PHP. Code example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators

Namespaces allow you to avoid name conflicts and group functionality. Within a namespace there can be
classes, interfaces, functions and constants are placed.

If a class, function, etc. is not located in any namespace, it is considered global.

Creating a namespace

To create a namespace, use the namespace directive followed by the namespace name:

<?php
namespace base;//create namespace

//everything below the namespace belongs to it
class Person
{
    private $name;
    function __construct($name) { 
        $this->name = $name; 
    }
}

?>
! The namespace definition must be placed above any other HTML code or markup.

Appeal to the namespace

A special construction /namespace/Classname is used to refer to the namespace:

$tom = new \base\Person("Victor");

In your projects, we recommend using the /App/Classname structure to name projects. Accordingly, the class should be in the app folder.

Appeals from another class

file.php code example:

namespace base;

class Person
{
    public $name;
    function __construct($name) { $this->name = $name; }
}

Now, in another file, we include this file and refer to the Person class:

    namespace work;
    
    include "Person.php";
    $tom = new \base\Person("Tom");
    echo $tom->name;

Some namespaces can contain others:

namespace base\classes;

Aliases

Calling classes with their namespaces makes our code harder to read and cumbersome.

In this case, we can use aliases, which are given in the form:

use full_classname_with_namespaces as pseudonym;
<?php
namespace work;
include "person.php";
 
use \base\classes\Person as User;
 
$tom = new User("Tom");

$bob = new User("Bob");

?>

You can only use the use construct without specifying an alias. Then the class can be used according to its
direct name:

use \base\classes\Person;

$tom = new Person("Tom");
$bob = new Person("Bob");

Several classes at once:

use \base\classes\Person as User, \base\classes\Employee as Employee;


use \base\classes\{Person as User, Employee as Employee};

Connecting Constants and Functions

The const statement is used to connect constants, and “use function” is used to connect functions.

//create namespace
namespace base\classes;


const admin = "Garry";


function printPerson($person){
     
    echo $person->name;
}

class Person {
    public $name;
    function __construct($name) { 
        $this->name = $name; 
    }
}

Connect this file to another:

//in this file we have another namespace
namespace work;
include "admin.php";
 
//allow you to use the Person class in this file with others namespace
use \base\classes\Person;
//allow to use the admin constant without specifying namespaces
use const \base\classes\admin;
//allow to use function printPerson without specifying namespaces
use function \base\classes\printPerson;
 
$tom = new Person(adminName);
printPerson($evan);  
?>
The autoload function allows you to manage the namespace more conveniently, and in large projects this functionality is performed by Composer.
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation