国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP面向?qū)ο蟪绦蛟O(shè)計(jì)之命名空間與自動加載類詳解

2024-05-04 23:53:10
字體:
供稿:網(wǎng)友

本文實(shí)例講述了PHP面向?qū)ο蟪绦蛟O(shè)計(jì)之命名空間與自動加載類。分享給大家供大家參考,具體如下:

命名空間

避免類名重復(fù),而產(chǎn)生錯誤。

<?phprequire_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(); // 同一命名空間下,類名不能相同,默認(rèn)命名空間為空。空也是一種命名空間。$obj -> setName("Jack");print $obj->getName();//namespace useful; // 更改命名空間,否則查詢不到Hello類,Fatal error: Class 'my/Hello' not found$hello = new Hello();?><?php// useful/Outputter.phpnamespace useful; // 命名空間class Outputter {  //}class Hello {}?>

如何調(diào)用命名空間中的類

<?phpnamespace 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關(guān)鍵字

<?phpnamespace 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 foundutil/Debug::helloWorld();?>

使用下面的處理,直接可以調(diào)用類

<?phpnamespace 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 namespaceclass Lister {  public static function helloWorld() {    print "hello from global/n";  }}?><?phpnamespace com/getinstance/util;require_once 'global.php';class Lister {  public static function helloWorld() {    print "hello from ".__NAMESPACE__."/n"; // __NAMESPACE__當(dāng)前namespace  }}Lister::helloWorld(); // access local/Lister::helloWorld(); // access global?>

輸出:

hello from com/getinstance/util
hello from global

命名空間加{}

<?phpnamespace com/getinstance/util {  class Debug {    static function helloWorld() {      print "hello from Debug/n";    }  }}namespace main {  /com/getinstance/util/Debug::helloWorld();}?>

output:

hello from Debug

全局命名空間

<?phpnamespace { // 全局空間  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

<?phpclass ShopProduct {  function __construct() {    print "ShopProduct constructor/n";  }}?><?phpfunction __autoload( $classname ) { // 自動加載,根據(jù)類名加載類  include_once( "$classname.php" );}$product = new ShopProduct( 'The Darkening', 'Harry', 'Hunter', 12.99 );?>

output:

ShopProduct constructor

進(jìn)一步優(yōu)化處理

位于文件夾business/ShopProduct.php

<?phpclass business_ShopProduct { // 這里的類命名就要遵循規(guī)則了  function __construct() {    print "business_ShopProduct constructor/n";  }}?><?phpfunction __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程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 漳州市| 景谷| 兰考县| 曲阜市| 小金县| 文安县| 巨野县| 福清市| 浦县| 榆林市| 昌邑市| 东乌珠穆沁旗| 平昌县| 大城县| 乌苏市| 天门市| 大方县| 天全县| 拜城县| 满城县| 南平市| 嘉峪关市| 黄大仙区| 全南县| 博野县| 招远市| 太原市| 罗田县| 通城县| 福建省| 东乡| 贺州市| 深圳市| 威海市| 德兴市| 洛南县| 沁水县| 巢湖市| 大冶市| 新宁县| 青川县|