本文的示例代码参考namespace
目录
开始
mkdir -p vendor/lib1 && vim vendor/lib1/Person.php
<?php
class Person
{
var $name, $age;
function __construct($name, $age)
{
echo "__construct of lib1/Person.php" . "\n";
$this->name = $name;
$this->age = $age;
}
}
PHP语法参考 X分钟速成Y - 其中 Y=PHP
vim startup.php
<?php
require_once "vendor/lib1/Person.php";
$person = new Person("David", 6);
var_dump($person);
php startup.php
__construct of lib1/Person.php
object(Person)#1 (2) {
["name"]=>
string(5) "David"
["age"]=>
int(6)
}
PHP环境搭建参考 PHP学习 之 autoload
命名冲突
mkdir -p vendor/lib2 && vim vendor/lib2/Person.php
<?php
class Person
{
var $name, $age;
function __construct($name, $age)
{
echo "__construct of lib2/Person.php" . "\n";
$this->name = $name;
$this->age = $age;
}
}
vim conflict.php
<?php
require_once "vendor/lib1/Person.php";
require_once "vendor/lib2/Person.php";
$person = new Person("David", 6);
var_dump($person);
php conflict.php
PHP Fatal error: Cannot declare class Person, because the name is already in use in /Users/yuanlin/Workspace/php-tutorial/namespace/vendor/lib2/Person.php on line 2
命名空间
- vendor/lib1/Person.php
vim vendor/lib1/Person.php
<?php
namespace lib1;
class Person
{
var $name, $age;
function __construct($name, $age)
{
echo "__construct of lib1/Person.php" . "\n";
$this->name = $name;
$this->age = $age;
}
}
echo "namespace: ". __NAMESPACE__ . "\n";
php vendor/lib1/Person.php
namespace: lib1
常量
__NAMESPACE__
的值是包含当前命名空间名称的字符串
- vendor/lib2/Person.php
vim vendor/lib2/Person.php
<?php
namespace lib2;
class Person
{
var $name, $age;
function __construct($name, $age)
{
echo "__construct of lib2/Person.php" . "\n";
$this->name = $name;
$this->age = $age;
}
}
echo "namespace: ". __NAMESPACE__ . "\n";
php vendor/lib2/Person.php
namespace: lib2
- namespace.php
vim namespace.php
<?php
require_once "vendor/lib1/Person.php";
require_once "vendor/lib2/Person.php";
$person = new lib1\Person("David", 6);
var_dump($person);
$person = new lib2\Person("David", 6);
var_dump($person);
echo "namespace: ". __NAMESPACE__ . "\n";
php namespace.php
namespace: lib1
namespace: lib2
__construct of lib1/Person.php
object(lib1\Person)#1 (2) {
["name"]=>
string(5) "David"
["age"]=>
int(6)
}
__construct of lib2/Person.php
object(lib2\Person)#2 (2) {
["name"]=>
string(5) "David"
["age"]=>
int(6)
}
namespace:
在全局的即不包括在任何命名空间中的代码 常量
__NAMESPACE__
的值是包含一个空的字符串
全局空间
在名称前加上前缀\表示该名称是全局空间中的名称
vim global.php
<?php
echo strlen('123') . "\n";
echo \strlen('123') . "\n";
php global.php
3
3
引用规则
非限定名称
vim regular_1.php
<?php
namespace app;
require_once "vendor/lib1/Person.php";
require_once "vendor/lib2/Person.php";
$person = new Person("David", 6);
var_dump($person);
php regular_1.php
PHP Fatal error: Uncaught Error: Class 'app\Person' not found in /Users/yuanlin/Workspace/php-tutorial/namespace/regular_1.php:8
非限定名称引用: current namespace + 非限定名称
PS: 非限定名称的详细定义参考 使用命名空间:基础
限定名称
vim regular_2.php
<?php
namespace app;
require_once "vendor/lib1/Person.php";
require_once "vendor/lib2/Person.php";
$person = new lib1\Person("David", 6);
var_dump($person);
php regular_2.php
PHP Fatal error: Uncaught Error: Class 'app\lib1\Person' not found in /Users/yuanlin/Workspace/php-tutorial/namespace/regular_2.php:8
限定名称引用: current namespace + 限定名称
完全限定名称
vim regular_3.php
<?php
namespace app;
require_once "vendor/lib1/Person.php";
require_once "vendor/lib2/Person.php";
$person = new \lib1\Person("David", 6);
var_dump($person);
php regular_3.php
__construct of lib1/Person.php
object(lib1\Person)#1 (2) {
["name"]=>
string(5) "David"
["age"]=>
int(6)
}
完全限定名称引用: 完全限定名称
引用别名
vim alias.php
<?php
require_once "vendor/lib1/Person.php";
use lib1\Person as User;
$user = new User("David", 6);
var_dump($user);
php alias.php
__construct of lib1/Person.php
自动加载
PSR-0
PSR (PHP Standards Recommendations)
FIG (PHP Framework Interop Group) - Moving PHP forward through collaboration and standards
Deprecated - As of 2014-10-21 PSR-0 has been marked as deprecated. PSR-4 is now recommended
as an alternative
PSR-4
mkdir -p autoload/psr4 && cd autoload/psr4
vim composer.json
{
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
}
mkdir -p app/Core && vim app/Core/Request.php
<?php
namespace App\Core;
class Request {
function __construct()
{
echo "__construct of App\Core\Request.php" . "\n";
}
}
echo "namespace: ". __NAMESPACE__ . "\n";
composer update
vim psr4.php
<?php
require __DIR__.'/vendor/autoload.php';
use App\Core\Request;
$request = new Request();
php psr4.php
namespace: App\Core
__construct of App\Core\Request.php
classmap
cd ../..
mkdir -p autoload/classmap && cd autoload/classmap
vim composer.json
{
"autoload": {
"classmap": [
"app"
]
}
}
mkdir -p app/Core && vim app/Core/Request.php
<?php
namespace App\Core;
class Request {
function __construct()
{
echo "__construct of App\Core\Request.php" . "\n";
}
}
echo "namespace: ". __NAMESPACE__ . "\n";
composer update
vim classmap.php
<?php
require __DIR__.'/vendor/autoload.php';
use App\Core\Request;
$request = new Request();
php psr4.php
namespace: App\Core
__construct of App\Core\Request.php
classmap与PSR-4这两种autoload方式有何区别?
files
cd ../..
mkdir -p autoload/files && cd autoload/files
vim composer.json
{
"autoload": {
"files": [
"app/Core/Request.php"
]
}
}
mkdir -p app/Core && vim app/Core/Request.php
<?php
namespace App\Core;
class Request {
function __construct()
{
echo "__construct of App\Core\Request.php" . "\n";
}
}
echo "namespace: ". __NAMESPACE__ . "\n";
composer update
vim files.php
<?php
require __DIR__.'/vendor/autoload.php';
use App\Core\Request;
$request = new Request();
php files.php
namespace: App\Core
__construct of App\Core\Request.php
files这种autoload方式的应用场景?