PHP面向对象程序设计之命名空间与自动加载类详解
本文实例讲述了PHP面向对象程序设计之命名空间与自动加载类。分享给大家供大家参考,具体如下:
命名空间
避免类名重复,而产生错误。
<?php
require_once"useful/Outputter.php";
classOutputter{
//outputdata
private$name;
publicfunctionsetName($name){
$this->name=$name;
}
publicfunctiongetName(){
return$this->name;
}
}
$obj=newOutputter();//同一命名空间下,类名不能相同,默认命名空间为空。空也是一种命名空间。
$obj->setName("Jack");
print$obj->getName();
//namespaceuseful;//更改命名空间,否则查询不到Hello类,Fatalerror:Class'my\Hello'notfound
$hello=newHello();
?>
<?php
//useful/Outputter.php
namespaceuseful;//命名空间
classOutputter{
//
}
classHello{
}
?>
如何调用命名空间中的类
<?php
namespacecom\getinstance\util;
classDebug{
staticfunctionhelloWorld(){
print"hellofromDebug\n";
}
}
namespacemain;
//com\getinstance\util\Debug::helloWorld();//找不到Debug类
\com\getinstance\util\Debug::helloWorld();//加斜杠之后,就从根部去寻找了。
//outPut:hellofromDebug
?>
使用use关键字
<?php
namespacecom\getinstance\util;
classDebug{
staticfunctionhelloWorld(){
print"hellofromDebug\n";
}
}
namespacemain;
usecom\getinstance\util;
//Debug::helloWorld();//Fatalerror:Class'main\Debug'notfound
util\Debug::helloWorld();
?>
使用下面的处理,直接可以调用类
<?php
namespacecom\getinstance\util;
classDebug{
staticfunctionhelloWorld(){
print"hellofromDebug\n";
}
}
namespacemain;
usecom\getinstance\util\Debug;//直接使用到类
Debug::helloWorld();
?>
\表示全局
global.php
<?php
//nonamespace
classLister{
publicstaticfunctionhelloWorld(){
print"hellofromglobal\n";
}
}
?>
<?php
namespacecom\getinstance\util;
require_once'global.php';
classLister{
publicstaticfunctionhelloWorld(){
print"hellofrom".__NAMESPACE__."\n";//__NAMESPACE__当前namespace
}
}
Lister::helloWorld();//accesslocal
\Lister::helloWorld();//accessglobal
?>
输出:
hellofromcom\getinstance\util
hellofromglobal
命名空间加{}
<?php
namespacecom\getinstance\util{
classDebug{
staticfunctionhelloWorld(){
print"hellofromDebug\n";
}
}
}
namespacemain{
\com\getinstance\util\Debug::helloWorld();
}
?>
output:
hellofromDebug
全局命名空间
<?php
namespace{//全局空间
classLister{
publicstaticfunctionhelloWorld(){
print"hellofromglobal\n";
}
}
}
namespacecom\getinstance\util{
classLister{
publicstaticfunctionhelloWorld(){
print"hellofrom".__NAMESPACE__."\n";
}
}
Lister::helloWorld();//accesslocal
\Lister::helloWorld();//accessglobal
}
?>
__autoload自动加载类
ShopProduct.php
<?php
classShopProduct{
function__construct(){
print"ShopProductconstructor\n";
}
}
?>
<?php
function__autoload($classname){//自动加载,根据类名加载类
include_once("$classname.php");
}
$product=newShopProduct('TheDarkening','Harry','Hunter',12.99);
?>
output:
ShopProductconstructor
进一步优化处理
位于文件夹business/ShopProduct.php
<?php
classbusiness_ShopProduct{//这里的类命名就要遵循规则了
function__construct(){
print"business_ShopProductconstructor\n";
}
}
?>
<?php
function__autoload($classname){
$path=str_replace('_',DIRECTORY_SEPARATOR,$classname);//智能化处理
require_once("$path.php");
}
$x=newShopProduct();
$y=newbusiness_ShopProduct();
?>
output:
ShopProductconstructor
business_ShopProductconstructor
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。