php命名空间与自动加载类用法实例详解

伊谢尔伦
Lepaskan: 2023-03-12 06:56:01
asal
1164 orang telah melayarinya

这篇文章主要介绍了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 {
}
?>
Salin selepas log masuk

如何调用命名空间中的类

<?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
?>
Salin selepas log masuk

使用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();
?>
Salin selepas log masuk

使用下面的处理,直接可以调用类

<?php
namespace com\getinstance\util;
class Debug {
  static function helloWorld() {
    print "hello from Debug\n";
  }
}
namespace main;
use com\getinstance\util\Debug; // 直接使用到类
Debug::helloWorld();
?>
Salin selepas log masuk

\表示全局

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
?>
Salin selepas log masuk

输出:

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();
}
?>
Salin selepas log masuk

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
}
?>
Salin selepas log masuk

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 );
?>
Salin selepas log masuk

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();
?>
Salin selepas log masuk

output:

ShopProduct constructor
business_ShopProduct constructor

Atas ialah kandungan terperinci php命名空间与自动加载类用法实例详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!