程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> PHP面向對象程序設計之命名空間與自動加載類詳解

PHP面向對象程序設計之命名空間與自動加載類詳解

編輯: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();
?>
<?php
// useful/Outputter.php
namespace useful; // 命名空間
class Outputter {
  //
}
class Hello {
}
?>

如何調用命名空間中的類

<?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關鍵字

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

使用下面的處理,直接可以調用類

<?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__當前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 自動加載類

ShopProduct.php

<?php
class ShopProduct {
  function __construct() {
    print "ShopProduct constructor\n";
  }
}
?>
<?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 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

更多關於PHP相關內容感興趣的讀者可查看本站專題:《php面向對象程序設計入門教程》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《PHP網絡編程技巧總結》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

希望本文所述對大家PHP程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved