PHP面向?qū)ο蟪绦蛟O(shè)計(jì)之命名空間與自動(dòng)加載類的示例分析-創(chuàng)新互聯(lián)

小編給大家分享一下PHP面向?qū)ο蟪绦蛟O(shè)計(jì)之命名空間與自動(dòng)加載類的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

成都創(chuàng)新互聯(lián)公司致力于成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì),成都網(wǎng)站設(shè)計(jì),集團(tuán)網(wǎng)站建設(shè)等服務(wù)標(biāo)準(zhǔn)化,推過(guò)標(biāo)準(zhǔn)化降低中小企業(yè)的建站的成本,并持續(xù)提升建站的定制化服務(wù)水平進(jìn)行質(zhì)量交付,讓企業(yè)網(wǎng)站從市場(chǎng)競(jìng)爭(zhēng)中脫穎而出。 選擇成都創(chuàng)新互聯(lián)公司,就選擇了安全、穩(wěn)定、美觀的網(wǎng)站建設(shè)服務(wù)!

具體如下:

命名空間

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

<?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(); // 同一命名空間下,類名不能相同,默認(rèn)命名空間為空。空也是一種命名空間。
$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 {
}
?>

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

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

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

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

<?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__當(dāng)前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 自動(dòng)加載類

ShopProduct.php

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

output:

ShopProduct constructor

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

位于文件夾business/ShopProduct.php

<?php
class business_ShopProduct { // 這里的類命名就要遵循規(guī)則了
  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();
?>

以上是“PHP面向?qū)ο蟪绦蛟O(shè)計(jì)之命名空間與自動(dòng)加載類的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

新聞標(biāo)題:PHP面向?qū)ο蟪绦蛟O(shè)計(jì)之命名空間與自動(dòng)加載類的示例分析-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)鏈接:http://bm7419.com/article24/dpdice.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)營(yíng)銷型網(wǎng)站建設(shè)、軟件開(kāi)發(fā)服務(wù)器托管、建站公司、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)