微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP面向对象程序设计之命名空间与自动加载类详解

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教程

<?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教程

<?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教程

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

\表示全局PHP教程

global.PHPPHP教程

<?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
?>

输出PHP教程

hello from com\getinstance\util
hello from globalPHP教程

命名空间加{}PHP教程

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

output:PHP教程

hello from DebugPHP教程

全局命名空间PHP教程

<?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 自动加载类PHP教程

ShopProduct.PHPPHP教程

<?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:PHP教程

ShopProduct constructorPHP教程

进一步优化处理PHP教程

位于文件夹business/ShopProduct.PHPPHP教程

<?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:PHP教程

ShopProduct constructor
business_ShopProduct constructorPHP教程

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP面向对象程序设计入门教程》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《PHP字符串(string)用法总结》、《PHP+MysqL数据库操作入门教程》及《PHP常见数据库操作技巧汇总》PHP教程

希望本文所述对大家PHP程序设计有所帮助.PHP教程

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐