欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2016年12月02日 10:17:33   作者:牛逼的霍嘯林  
這篇文章主要介紹了PHP面向?qū)ο蟪绦蛟O(shè)計(jì)之命名空間與自動(dòng)加載類(lèi),結(jié)合實(shí)例形式分析了php命名空間與自動(dòng)加載類(lèi)的概念、功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下

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

命名空間

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

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

<?php
namespace com\getinstance\util;
class Debug {
  static function helloWorld() {
    print "hello from Debug\n";
  }
}
namespace main;
// com\getinstance\util\Debug::helloWorld(); // 找不到Debug類(lèi)
\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)用類(lèi)

<?php
namespace com\getinstance\util;
class Debug {
  static function helloWorld() {
    print "hello from Debug\n";
  }
}
namespace main;
use com\getinstance\util\Debug; // 直接使用到類(lèi)
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)加載類(lèi)

ShopProduct.php

<?php
class ShopProduct {
  function __construct() {
    print "ShopProduct constructor\n";
  }
}
?>
<?php
function __autoload( $classname ) { // 自動(dòng)加載,根據(jù)類(lèi)名加載類(lèi)
  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 { // 這里的類(lèi)命名就要遵循規(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();
?>

output:

ShopProduct constructor
business_ShopProduct constructor

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP基本語(yǔ)法入門(mén)教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論