本文实例讲述了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 'myhello' not found
$hello = new hello();
?>
<?php
// useful/outputter.php
namespace useful; // 命名空间
class outputter {
//
}
class hello {
}
?>
如何调用命名空间中的类
<?php
namespace comgetinstanceutil;
class debug {
static function helloworld() {
print "hello from debugn";
}
}
namespace main;
// comgetinstanceutildebug::helloworld(); // 找不到debug类
comgetinstanceutildebug::helloworld(); // 加斜杠之后,就从根部去寻找了。
// output:hello from debug
?>
使用use关键字
<?php
namespace comgetinstanceutil;
class debug {
static function helloworld() {
print "hello from debugn";
}
}
namespace main;
use comgetinstanceutil;
//debug::helloworld(); //fatal error: class 'maindebug' not found
utildebug::helloworld();
?>
使用下面的处理,直接可以调用类
<?php
namespace comgetinstanceutil;
class debug {
static function helloworld() {
print "hello from debugn";
}
}
namespace main;
use comgetinstanceutildebug; // 直接使用到类
debug::helloworld();
?>
表示全局
global.php
<?php
// no namespace
class lister {
public static function helloworld() {
print "hello from globaln";
}
}
?>
<?php
namespace comgetinstanceutil;
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 comgetinstanceutil
hello from global
命名空间加{}
<?php
namespace comgetinstanceutil {
class debug {
static function helloworld() {
print "hello from debugn";
}
}
}
namespace main {
comgetinstanceutildebug::helloworld();
}
?>
output:
hello from debug
全局命名空间
<?php
namespace { // 全局空间
class lister {
public static function helloworld() {
print "hello from globaln";
}
}
}
namespace comgetinstanceutil {
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 constructorn";
}
}
?>
<?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 constructorn";
}
}
?>
<?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相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《php基本语法入门教程》、《php运算与运算符用法总结》、《php网络编程技巧总结》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!