这篇文章主要介绍了php面向对象程序设计之命名空间与自动加载类,结合实例形式分析了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();
}
?>
output:
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 );
?>
output:
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();
?>
output:
shopproduct constructor
business_shopproduct constructor
以上就是php命名空间与自动加载类用法实例详解的详细内容。