PHP namespace

Keywords: PHP

1. background

test1.php

<?php
class IMooc{

}
?>

test2.php

<?php
class IMooc{
    
}
?>

Introducing the above two files test1.php and test2.php in namespace.php will cause an error and declare the same class

<?php
require('test1.php');
require('test2.php');
// Fatal error: Cannot redeclare class IMooc 

Therefore, in order to solve the above problems, the concept of namespace is introduced. The PHP namespace is added in PHP 5.3, and the namespace is declared by the keyword namespace.

2. Next, you will use namespaces to modify the above code

test1.php

<?php
namespace Test1;
class IMooc{

}
?>

test2.php

<?php
namespace Test2;
class IMooc{
    
}
?>

There is no error when two test1.php and test2.php are loaded in namespace.php like this

<?php
require('test1.php');
require('test2.php');

 

3. Use namespaces

Note: The following types are affected by namespaces.

  • Classes (including Abstract classes, traits), interfaces, functions, and constants

[1] Instantiate classes using namespaces

<?php
require('test1.php');
require('test2.php');
var_dump(new Test1\IMooc());  //object(Test1\IMooc)#1 (0) { } 
var_dump(new Test2\IMooc());  //object(Test2\IMooc)#1 (0) { } 

[2] Functions use namespaces

Define a function in test1.php

<?php
namespace Test1;
function iLikeMooc(){
    echo "i like mooc";
}
?>

Use namespace to call functions in namespace.php

<?php
require('test1.php');
require('test2.php');
Test1\iLikeMooc();//i like mooc

[3] Constant, define keyword is not affected by namespace

Define a constant in test1.php

<?php
namespace Test1;
const IMooc='nice';
?>

Call constants in namespace.php using namespaces

<?php
require('test1.php');
require('test2.php');
echo Test1\IMooc; //nice

4. Use the use keyword to import content in the namespace and alias the namespace

[1] Use use to import namespaces and use them

test1.php

<?php
namespace Test1;
class IMooc{

}
?>

Import Test1 IMooc in namespace.php and use

<?php
require('test1.php');
use Test1\IMooc;
var_dump(new IMooc()); //object(Test1\IMooc)#1 (0) { } 

[2] Namespaces are aliased to solve the problem of importing the same namespace (for example, classes)

test1.php

<?php
namespace Test1;
class IMooc{

}
?>

test2.php

<?php
namespace Test2;
class IMooc{
    
}
?>

namespace.php

<?php
use Test1\IMooc;
use Test2\IMooc as IMooc2; //Alias
require('test1.php');
require('test2.php');

var_dump(new IMooc());
var_dump(new IMooc2()); //Use aliases to instantiate

[3] Function, constant use as

test1.php

<?php
namespace Test1;
function iLikeMooc(){
    echo "i like mooc1";
}
const IMooc='nice1';
?>

test2.php

<?php
namespace Test2;
function iLikeMooc(){
    echo "i like mooc2";
}
const IMooc='nice2';
?>

namespace.php

<?php
require('test1.php');
require('test2.php');

use function Test1\iLikeMooc; //Function uses use 
use const Test1\IMooc; //Constant use
use function Test2\iLikeMooc as iLikeMooc2; //Functions use as
use const Test2\IMooc as IMooc2; //Constant use of as
//call
iLikeMooc();//i like mooc1
var_dump(IMooc);//nice1
iLikeMooc2();//i like mooc2
var_dump(IMooc2);//nice2

5. Global Namespaces and Namespaces

By default, no namespace is specified. It exists in the global namespace.

Question: We call functions and classes in the global namespace within the specified namespace

The namespace above is the global namespace, and test1.php and test2.php are the specified namespaces.

New file test3.php, no namespace specified

<?php
class Test1{


}
function test2(){
    echo "i like mooc2";
}
const test3='nice2';
?>

Create a new file index.php, specify a namespace, and call the contents of test3.php

<?php
namespace index;
include 'test3.php';
//var_dump(new Test1());  //Fatal error: Class 'index\Test1' not found 
var_dump(new \Test1());  //object(Test1)#1 (0) { } 
test2();// i like mooc2
echo test3;//nice2

When called in a specified namespace, it will first look in the current namespace, but not in the global namespace.

Posted by kaveman50 on Tue, 30 Apr 2019 01:00:37 -0700