php命名空間與自動載入類別用法實例詳解

伊谢尔伦
發布: 2023-03-12 06:56:01
原創
1165 人瀏覽過

這篇文章主要介紹了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 &#39;my\Hello&#39; 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 &#39;main\Debug&#39; 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 &#39;global.php&#39;;
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( &#39;The Darkening&#39;, &#39;Harry&#39;, &#39;Hunter&#39;, 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(&#39;_&#39;, DIRECTORY_SEPARATOR, $classname ); // 智能化处理
  require_once( "$path.php" );
}
$x = new ShopProduct();
$y = new business_ShopProduct();
?>
登入後複製
output:

#ShopProduct constructor

business_ShopProduct constructor

以上是php命名空間與自動載入類別用法實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!