PHP命名空间与自动加载类详解
本示例深入解析PHP面向对象编程中的命名空间定义与类的自动加载技术,供开发者参考学习。
关于命名空间的概念
其核心目的在于防止不同模块间类名发生冲突,从而引发程序错误。
<?php
require_once "useful/Outputter.php";
class Outputter {
// output data
private $name;
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
$obj = new Outputter(); // 同一命名空间下,类名不能相同,默认命名空间为空。空也是一种命名空间。
$obj -> setName("Jack");
print $obj->getName();
//namespace useful; // 更改命名空间,否则查询不到Hello类,Fatal error: Class 'my\Hello' not found
$hello = new Hello();
?>
<?php
// useful/Outputter.php
namespace useful; // 命名空间
class Outputter {
//
}
class Hello {
}
?>
在代码中调用命名空间内的类时
<?php
namespace com\getinstance\util;
class Debug {
static function helloWorld() {
print "hello from Debug\n";
}
}
namespace main;
// com\getinstance\util\Debug::helloWorld(); // 找不到Debug类
\com\getinstance\util\Debug::helloWorld(); // 加斜杠之后,就从根部去寻找了。
// outPut:hello from Debug
?>
可以借助use关键字进行导入
<?php
namespace com\getinstance\util;
class Debug {
static function helloWorld() {
print "hello from Debug\n";
}
}
namespace main;
use com\getinstance\util;
//Debug::helloWorld(); //Fatal error: Class 'main\Debug' not found
util\Debug::helloWorld();
?>
经过上述处理后,即可直接引用该类而无需写全限定名
<?php
namespace com\getinstance\util;
class Debug {
static function helloWorld() {
print "hello from Debug\n";
}
}
namespace main;
use com\getinstance\util\Debug; // 直接使用到类
Debug::helloWorld();
?>
反斜杠\代表全局作用域
对应源文件global.php
<?php
// no namespace
class Lister {
public static function helloWorld() {
print "hello from global\n";
}
}
?>
<?php
namespace com\getinstance\util;
require_once 'global.php';
class Lister {
public static function helloWorld() {
print "hello from ".__NAMESPACE__."\n"; // __NAMESPACE__当前namespace
}
}
Lister::helloWorld(); // access local
\Lister::helloWorld(); // access global
?>
运行结果如下:
hello from com\getinstance\util hello from global
命名空间支持使用{}包裹
<?php
namespace com\getinstance\util {
class Debug {
static function helloWorld() {
print "hello from Debug\n";
}
}
}
namespace main {
\com\getinstance\util\Debug::helloWorld();
}
?>
输出结果为:
hello from Debug
此外还有全局命名空间的使用场景
<?php
namespace { // 全局空间
class Lister {
public static function helloWorld() {
print "hello from global\n";
}
}
}
namespace com\getinstance\util {
class Lister {
public static function helloWorld() {
print "hello from ".__NAMESPACE__."\n";
}
}
Lister::helloWorld(); // access local
\Lister::helloWorld(); // access global
}
?>
利用__autoload函数实现类的自动加载
相关代码位于ShopProduct.php
<?php
class ShopProduct {
function __construct() {
print "ShopProduct constructor\n";
}
}
?>
<?php
function __autoload( $classname ) { // 自动加载,根据类名加载类
include_once( "$classname.php" );
}
$product = new ShopProduct( 'The Darkening', 'Harry', 'Hunter', 12.99 );
?>
执行输出:
ShopProduct constructor
对加载逻辑进行进一步优化
将文件置于business/ShopProduct.php目录下
<?php
class business_ShopProduct { // 这里的类命名就要遵循规则了
function __construct() {
print "business_ShopProduct constructor\n";
}
}
?>
<?php
function __autoload( $classname ) {
$path = str_replace('_', DIRECTORY_SEPARATOR, $classname ); // 智能化处理
require_once( "$path.php" );
}
$x = new ShopProduct();
$y = new business_ShopProduct();
?>
控制台输出:
ShopProduct constructor business_ShopProduct constructor
希望上述内容能对各位的PHP开发工作提供有益帮助。