• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 3年前 (2022-01-22) 15次浏览 已收录 0个评论

这篇文章主要介绍了PHP面向对象程序设计之命名空间与自动加载类,结合实例形式分析了php命名空间与自动加载类的概念、功能、使用方法与相关注意事项,需要的朋友可以参考下

本文实例讲述了PHP面向对象程序设计之命名空间与自动加载类。分享给大家供大家参考,具体如下:

命名空间

避免类名重复,而产生错误。

<?phprequire_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(); // 同一命名空间下,类名不能相同,默认命名空间为空。空也是一种命名空间。$ob<em style="color:transparent">本文来源[email protected]搞@^&代*@码)网9</em><strong>搞代gaodaima码</strong>j -> setName("Jack");print $obj->getName();//namespace useful; // 更改命名空间,否则查询不到Hello类,Fatal error: Class 'my\Hello' not found$hello = new Hello();?><?php// useful/Outputter.phpnamespace useful; // 命名空间class Outputter {  //}class Hello {}?>

如何调用命名空间中的类

<?phpnamespace 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关键字

<?phpnamespace 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 foundutil\Debug::helloWorld();?>

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

<?phpnamespace 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 namespaceclass Lister {  public static function helloWorld() {    print "hello from global\n";  }}?><?phpnamespace 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

命名空间加{}

<?phpnamespace com\getinstance\util {  class Debug {    static function helloWorld() {      print "hello from Debug\n";    }  }}namespace main {  \com\getinstance\util\Debug::helloWorld();}?>

output:

hello from Debug

全局命名空间

<?phpnamespace { // 全局空间  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

<?phpclass ShopProduct {  function construct() {    print "ShopProduct constructor\n";  }}?><?phpfunction autoload( $classname ) { // 自动加载,根据类名加载类  include_once( "$classname.php" );}$product = new ShopProduct( 'The Darkening', 'Harry', 'Hunter', 12.99 );?>

output:

ShopProduct constructor

进一步优化处理

位于文件夹business/ShopProduct.php

<?phpclass business_ShopProduct { // 这里的类命名就要遵循规则了  function construct() {    print "business_ShopProduct constructor\n";  }}?><?phpfunction 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命名空间与自动加载类用法实例详解的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php命名空间与自动加载类用法实例详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址