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();

?>

// useful/Outputter.php

namespace useful; // 命名空间

class Outputter {

//

}

class Hello {

}

?>

如何调用命名空间中的类

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关键字

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();

?>

使用下面的处理,直接可以调用类

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

// no namespace

class Lister {

public static function helloWorld() {

print "hello from global\n";

}

}

?>

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

命名空间加{}

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

全局命名空间

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

class ShopProduct {

function __construct() {

print "ShopProduct constructor\n";

}

}

?>

function __autoload( $classname ) { // 自动加载,根据类名加载类

include_once( "$classname.php" );

}

$product = new ShopProduct( 'The Darkening', 'Harry', 'Hunter', 12.99 );

?>

output:

ShopProduct constructor

进一步优化处理

位于文件夹business/ShopProduct.php

class business_ShopProduct { // 这里的类命名就要遵循规则了

function __construct() {

print "business_ShopProduct constructor\n";

}

}

?>

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程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值