PHP面向对象程序设计之命名空间与自动加载类详解
这里有新鲜出炉的 PHP 设计模式,程序狗速度看过来!
PHP 开源脚本语言
PHP(外文名: Hypertext Preprocessor,中文名:"超文本预处理器")是一种通用开源脚本语言。语法吸收了 C 语言、Java 和 Perl 的特点,入门门槛较低,易于学习,使用广泛,主要适用于 web 开发领域。PHP 的文件后缀名为 php。
这篇文章主要介绍了 PHP 面向对象程序设计之命名空间与自动加载类, 结合实例形式分析了 php 命名空间与自动加载类的概念、功能、使用方法与相关注意事项, 需要的朋友可以参考下
本文实例讲述了 PHP 面向对象程序设计之命名空间与自动加载类。分享给大家供大家参考,具体如下:
命名空间
避免类名重复, 而产生错误。
require_once"useful/Outputter.php";
classOutputter{
// output data
private$name;
publicfunctionsetName($name){
$this->name=$name;
}
publicfunctiongetName(){
return$this->name;
}
}
$obj=newOutputter();// 同一命名空间下,类名不能相同,默认命名空间为空。空也是一种命名空间。
$obj->setName("Jack");
print$obj->getName();
//namespace useful; // 更改命名空间,否则查询不到Hello类,Fatal error: Class 'my\Hello' not found
$hello=newHello();
?>
// useful/Outputter.php
namespaceuseful;// 命名空间
classOutputter{
//
}
classHello{
}
?>
如何调用命名空间中的类
namespacecom\getinstance\util;
classDebug{
staticfunctionhelloWorld(){
print"hello from Debug\n";
}
}
namespacemain;
// com\getinstance\util\Debug::helloWorld(); // 找不到Debug类
\com\getinstance\util\Debug::helloWorld();// 加斜杠之后,就从根部去寻找了。
// outPut:hello from Debug
?>
使用 use 关键字
namespacecom\getinstance\util;
classDebug{
staticfunctionhelloWorld(){
print"hello from Debug\n";
}
}
namespacemain;
usecom\getinstance\util;
//Debug::helloWorld(); //Fatal error: Class 'main\Debug' not found
util\Debug::helloWorld();
?>
使用下面的处理,直接可以调用类
namespacecom\getinstance\util;
classDebug{
staticfunctionhelloWorld(){
print"hello from Debug\n";
}
}
namespacemain;
usecom\getinstance\util\Debug;// 直接使用到类
Debug::helloWorld();
?>
\ 表示全局
global.php
// no namespace
classLister{
publicstaticfunctionhelloWorld(){
print"hello from global\n";
}
}
?>
namespacecom\getinstance\util;
require_once'global.php';
classLister{
publicstaticfunctionhelloWorld(){
print"hello from ".__NAMESPACE__."\n";// __NAMESPACE__当前namespace
}
}
Lister::helloWorld();// access local
\Lister::helloWorld();// access global
?>
输出:
hello from com\getinstance\util
hello from global
命名空间加 {}
namespacecom\getinstance\util{
classDebug{
staticfunctionhelloWorld(){
print"hello from Debug\n";
}
}
}
namespacemain{
\com\getinstance\util\Debug::helloWorld();
}
?>
output:
hello from Debug
全局命名空间
namespace{// 全局空间
classLister{
publicstaticfunctionhelloWorld(){
print"hello from global\n";
}
}
}
namespacecom\getinstance\util{
classLister{
publicstaticfunctionhelloWorld(){
print"hello from ".__NAMESPACE__."\n";
}
}
Lister::helloWorld();// access local
\Lister::helloWorld();// access global
}
?>
__autoload 自动加载类
ShopProduct.php
classShopProduct{
function__construct(){
print"ShopProduct constructor\n";
}
}
?>
function__autoload($classname){// 自动加载,根据类名加载类
include_once("$classname.php");
}
$product=newShopProduct('The Darkening','Harry','Hunter',12.99);
?>
output:
ShopProduct constructor
进一步优化处理
位于文件夹 business/ShopProduct.php
classbusiness_ShopProduct{// 这里的类命名就要遵循规则了
function__construct(){
print"business_ShopProduct constructor\n";
}
}
?>
function__autoload($classname){
$path=str_replace('_',DIRECTORY_SEPARATOR,$classname);// 智能化处理
require_once("$path.php");
}
$x=newShopProduct();
$y=newbusiness_ShopProduct();
?>
output:
ShopProduct constructor
business_ShopProduct constructor
希望本文所述对大家 PHP 程序设计有所帮助。
来源: http://www.phperz.com/article/17/0803/340958.html