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

 更新时间:2016年12月02日 10:17:33   作者:牛逼的霍啸林  
这篇文章主要介绍了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程序设计有所帮助。

相关文章

  • PHP检查空值的方法总结

    PHP检查空值的方法总结

    在本篇文章里小编给大家整理了一篇关于PHP检查空值的方法总结内容,有兴趣的朋友们可以学习参考下。
    2021-08-08
  • windows服务器使用IIS时thinkphp搜索中文无效问题

    windows服务器使用IIS时thinkphp搜索中文无效问题

    在用ThinkPHP开发的网站,在linux服务器下使用过一段时间,一切正常。但是更换到windows服务器时,发现搜索的时候,无法搜索中文,查不出相应的结果。查看数据库发现数据是存在的。linux服务器下正常,而且搜索数字或字母程序正常,说明程序是没有任何问题的。
    2023-06-06
  • php通过修改header强制图片下载的方法

    php通过修改header强制图片下载的方法

    这篇文章主要介绍了php通过修改header强制图片下载的方法,实例分析了php强制图片下载的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • PHP实现通过正则表达式替换回调的内容标签

    PHP实现通过正则表达式替换回调的内容标签

    这篇文章主要介绍了PHP实现通过正则表达式替换回调的内容标签的方法,涉及php正则匹配与替换的相关技巧,需要的朋友可以参考下
    2015-06-06
  • php使用NumberFormatter格式化货币的方法

    php使用NumberFormatter格式化货币的方法

    这篇文章主要介绍了php使用NumberFormatter格式化货币的方法,实例分析了php中NumberFormatter类的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • PHPLog php 程序调试追踪工具

    PHPLog php 程序调试追踪工具

    本文将为你介绍一个调试工具,它可以替代echo,print_r,var_dump等系统函数,还具有调用栈及参数追踪功能. 简言之,它是debug_backtrace的web版本.
    2009-09-09
  • PHP加密解密类实例分析

    PHP加密解密类实例分析

    这篇文章主要介绍了PHP加密解密类,实例分析了php实现加密与解密的原理与相关技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • WordPress中用于获取文章信息以及分类链接的函数用法

    WordPress中用于获取文章信息以及分类链接的函数用法

    这篇文章主要介绍了WordPress中用于获取文章信息以及分类链接的函数用法,分别是get_post()和get_category_link()的使用,需要的朋友可以参考下
    2015-12-12
  • PHP可逆加密/解密函数分享

    PHP可逆加密/解密函数分享

    很多项目的会员系统,都要求要有记住登录功能,在通过cookies实现功能是,由于要将客户信息直接保存到cookies,如果直接写入cookies势必会带来安全隐患,因此通过可逆加密后再保存到cookies相对就安全了
    2012-09-09
  • php读取文件内容到数组的方法

    php读取文件内容到数组的方法

    这篇文章主要介绍了php读取文件内容到数组的方法,涉及php中file、rtrim等函数对文件及字符串的操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03

最新评论