Namespaces



Namespaces ( namespace ) can be defined as structures showing the directory where the relevant class in simple terms. There is no inconvenience that two different files with the same name can be used in different directories. Namespaces also allow classes to be used. In other words, we can avoid overlapping classes with the same name in different indexes. In modern class architects, while each file is designed to contain a class, the namespace is also set to contain directory information. That's why we found it convenient to enter the subject with the phrase "building showing a sequence" . You will see how important the namespace is for the auto-uploading process we'll show later in terms of the location reporting of the class in question.

 

 

# Syntax


The namespace can also be used in files that contain functions or constants other than a class. However, it is widely preferred in class naming. The namespace is specified with the namespace id. In the demonstration of borrower \ brace is used.

<?php namespace Examples\Example;

class ExampleClass
{
    # your codes...
}

 

 

# Use Namespace


Now, in the following file, we have given a usage example that contains a namespace.

File: Example.php
<?php namespace Examples\Example;

class Test
{
    const name = __CLASS__;

    public function run()
    {
         return 'Run Class';
    }
}
File: index.php
<?php require 'Example.php';

$test = new Examples\Example\Test;

echo $test::name;
echo $test->run();
Examples \ Example \ Test
Run Class

 

 

# Notification of Name Field


If we take care of the above usage, we have defined the class with the name field. It is a bit difficult to repeat the same definition repeatedly when using the class Aany more than once. To overcome this situation, the name field must be specified in the file to be used with the use statement.

<?php require 'Example.php';

use Examples\Example\Test;

$test = new Test;

echo $test::name;
echo $test->run();
Examples \ Example \ Test
Run Class

 

 

# Nickname Usage


The name given to the file can be given by name if desired. The word as is used for this .

<?php require 'Example.php';

use Examples\Example\Test as T;

$test = new T;

echo $test::name;
echo $test->run();
Examples \ Example \ Test
Run Class

Once the nickname is given, you can no longer call with the original name.

 

 

# Use of Classes with Different Namespaces


If a class with a namespace uses a different class, it must be declared explicitly with use. Alternatively, the class used must be preceded by a \ symbol.

File: Example.php
<?php namespace Examples\Example;

use Test2;

class Test
{
    const name = __CLASS__;

    public function run()
    {
         return Test2::run();
    }
}

If the calling class contains a namespace, it must be declared with the namespace.

<?php namespace Examples\Example;

use Examples2\Example\Test2;

class Test
{
    const name = __CLASS__;

    public function run()
    {
         return Test2::run();
    }
}

 

 

# Use of Classes That Have the Same Namespace


The object of the file with the same name used in the area during a one of a user need not be notified ..

File: Example.php
<?php namespace Examples\Example;

class Test2
{
    public static function run()
    {
        return 'Run Class';
    }
}

class Test
{
    const name = __CLASS__;

    public function run()
    {
         return Test2::run();
    }
}

 

 

# Use of the same Islays


In spite of different names, we should pay attention when using the same named classes in one. Name area with a free domain name or nickname while class should be included in the same class or named \ it should be preceded by the symbol.

<?php namespace Examples\Example;

class Test extends \Test
{
    const name = __CLASS__;

    public function run()
    {
         return 'Run Class';
    }
}

Or it must be indicated by a nickname.

<?php namespace Examples\Example;

use Test as T;

class Test extends T
{
    const name = __CLASS__;

    public function run()
    {
         return 'Run Class';
    }
}