Web 开发PHP经验分享PHP实战

php初级讲义11-命名空间

2017-05-03  本文已影响41人  Stone_Zhuo

命名空间的概念

命名空间是一种封装事物的方法。

class Person {}

$stone = new Person;
echo '<pre>';
print_r($stone);
echo '</pre>';
/*
Person Object
(
)
*/
namespace name\space;

class Person {}

$stone = new Person;
echo '<pre>';
print_r($stone);
echo '</pre>';

$tom = new \name\space\Person;
echo '<pre>';
print_r($tom);
echo '</pre>';

function say_hello() {}

const COUNT = 1;

var_dump(COUNT);
echo '<br/>';
var_dump(\name\space\COUNT);
echo '<br/>';
var_dump(namespace\COUNT);
echo '<br/>';
var_dump(__NAMESPACE__);
echo '<br/>';
var_dump(__NAMESPACE__.'\COUNT');
echo '<br/>';
echo constant(__NAMESPACE__.'\COUNT');
/*
name\space\Person Object
(
)

name\space\Person Object
(
)

int(1)
int(1)
int(1)
string(10) "name\space"
string(16) "name\space\COUNT"
1
*/
<html>
<?php
    namespace MyNamespace;
// Fatal error: Namespace declaration statement has to be the very first statement in the script in
namespace MyNamespace;
const COUNT = 1;
var_dump(COUNT);
echo '<br/>';

namespace YourNamespace;
CONST COUNT = 2;
var_dump(COUNT);

/*
int(1) 
int(2)
*/

// 以上属于是单个文件多个命名空间的简单组合语法形式
namespace MyNamespace {
    const COUNT = 1;
    var_dump(COUNT);
    echo '<br/>';
}

namespace YourNamespace {
    CONST COUNT = 2;
    var_dump(COUNT);
}

/*
int(1) 
int(2)
*/

// 以上属于是单个文件多个命名空间的大括号语法形式,比简单组合语法形式更值得推荐
namespace MyProject {
    function hello()
    {
        echo 'hello';
    }
    hello(); // hello
    echo '<br/>';
}

namespace {
    // hello(); // Fatal error: Call to undefined function hello() ini
    MyProject\hello(); // hello
}

命名空间的使用

命名空间中的元素有三种使用方式

// Person.php
namespace Project\App\Model;

class Person
{
    public static function hello()
    {
        echo 'hello in Project\App\Model';
        echo '<br/>';
    }
}
namespace Project\App;

require_once 'Person.php';

class Person
{
    public static function hello()
    {
        echo 'hello in Project\App';
        echo '<br/>';
    }
}

Person::hello(); // hello in Project\App

Model\Person::hello(); // hello in Project\App\Model

\Project\App\Person::hello(); // hello in Project\App

\Project\App\Model\Person::hello(); // hello in Project\App\Model

echo \strlen('hello'); // 5
echo '<br/>';
echo \PHP_INT_MAX; // 9223372036854775807
echo '<br/>';
echo strlen('hello'); // 5
echo '<br/>';
echo PHP_INT_MAX; // 9223372036854775807
echo '<br/>';
// Person.php
namespace Project\App\Model;

const COUNT = 'this is a constant in Project\App\Model';

function test()
{
    echo 'this is a function in Project\App\Model';
    echo '<br/>';
}

class Person
{
    public static function hello()
    {
        echo 'hello in Project\App\Model';
        echo '<br/>';
    }
}
namespace Project\App;

require_once 'Person.php';

const COUNT = 'this is a constant in Project\App';

function test()
{
    echo 'this is a function in Project\App';
    echo '<br/>';
}

class Person
{
    public static function hello()
    {
        echo 'hello in Project\App';
        echo '<br/>';
    }
}

echo COUNT; // this is a constant in Project\App
echo '<br/>';
$count = 'COUNT';
echo constant($count); // Warning: constant(): Couldn't find constant COUNT in
echo '<br/>';
$count = 'Project\App\COUNT';
echo constant($count); // this is a constant in Project\App
echo '<br/>';
test(); // this is a function in Project\App
$function_name = 'test';
// $function_name(); // Fatal error: Call to undefined function test() in
$function_name = 'Project\App\Model\test';
$function_name(); // this is a function in Project\App\Model
Person::hello(); // hello in Project\App
$class_name = 'Person';
//$class_name::hello(); // Fatal error: Class 'Person' not found in
$class_name = '\Project\App\Person';
$class_name::hello(); // hello in Project\App
var_dump(__NAMESPACE__); // string(0) ""
namespace App\Http\Controller;

echo __NAMESPACE__; // App\Http\Controller
// Person.php
namespace Project\App\Model;

const COUNT = 'this is a constant in Project\App\Model';

function test()
{
    echo 'this is a function in Project\App\Model';
    echo '<br/>';
}

class Person
{
    public static function hello()
    {
        echo 'hello in Project\App\Model';
        echo '<br/>';
    }
}
namespace Project\App;

require_once 'Person.php';

const COUNT = 1;

namespace\Model\test(); // this is a function in Project\App\Model

echo namespace\COUNT; // 1
// Person.php
namespace Project\App\Model;

const COUNT = 'this is a constant in Project\App\Model';

function test()
{
    echo 'this is a function in Project\App\Model';
    echo '<br/>';
}

class Person
{
    public static function hello()
    {
        echo 'hello in Project\App\Model';
        echo '<br/>';
    }
}

class Programmer extends Person
{
    public static function code()
    {
        echo 'I am coding...';
        echo '<br/>';
    }
}
namespace Project\App;
ini_set('display_errors', 'on');
error_reporting(E_ALL);
require_once 'Person.php';

use Project\App\Model\Programmer as Prog;
Prog::code(); // I am coding...

use Project\App\Model\Person; // 词句与use Project\App\Model\Person as Person等价;
Person::hello(); // hello in Project\App\Model

use function Project\App\Model\test; // php5.6+
test(); // this is a function in Project\App\Model

use const Project\App\Model\COUNT; // php5.6+
echo COUNT; // this is a constant in Project\App\Model
echo '<br/>';

use Project\App as App;
App\Model\test(); // this is a function in Project\App\Model
// Person.php
namespace Project\App\Model;

const COUNT = 'this is a constant in Project\App\Model';

function test()
{
    echo 'this is a function in Project\App\Model';
    echo '<br/>';
}

class Person
{
    public static function hello()
    {
        echo 'hello in Project\App\Model';
        echo '<br/>';
    }
}

class Programmer extends Person
{
    public static function code()
    {
        echo 'I am coding...';
        echo '<br/>';
    }
}
namespace Project\App;
ini_set('display_errors', 'on');
error_reporting(E_ALL);
require_once 'Person.php';

use Project\App\Model\Person, Project\App\Model\Programmer as Prog;

Person::hello(); // hello in Project\App\Model
Prog::code(); // I am coding...
// Person.php
namespace Project\App\Model;

const COUNT = 'this is a constant in Project\App\Model';

function test()
{
    echo 'this is a function in Project\App\Model';
    echo '<br/>';
}

class Person
{
    public static function hello()
    {
        echo 'hello in Project\App\Model';
        echo '<br/>';
    }
}

class Programmer extends Person
{
    public static function code()
    {
        echo 'I am coding...';
        echo '<br/>';
    }
}
namespace Project\App;
ini_set('display_errors', 'on');
error_reporting(E_ALL);
require_once 'Person.php';

use Project\App\Model\Person;
Person::hello(); // hello in Project\App\Model
$class_name = 'Person';
$class_name::hello(); // Fatal error: Class 'Person' not found in

本文首发于公众号:programmer_cc,转载请注明出处。


微信公众号.jpg
上一篇 下一篇

猜你喜欢

热点阅读