命名空间

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

<?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面向对象深入研究之【命名空间】与【自动加载类】的更多相关文章

  1. PHP命名空间与自动加载类详解

    本文实例讲述了PHP命名空间与自动加载类.分享给大家供大家参考,具体如下: 今天我要给大家介绍的是PHP的命名空间 和 自动加载类 我先简单的分开演示 在放在一起 大家请看:什么是自动加载类? 想必大 ...

  2. PHP自动加载类__autoload()浅谈

    在面向对象编程中,都是以对象为单位的操作,如果我有两个不同的类,类A和类B,在同一个文件里,实例化对象,就能在这个文件同时调用类A和类B的函数 <?php #a.php class A{ pub ...

  3. PHP 进阶篇:面向对象的设计原则,自动加载类,类型提示,traits,命名空间,spl的使用,反射的使用,php常用设计模式 (麦子学员 第三阶段)

    以下是进阶篇的内容:面向对象的设计原则,自动加载类,类型提示,traits,命名空间,spl的使用,反射的使用,php常用设计模式 ================================== ...

  4. 【PHP面向对象(OOP)编程入门教程】23.自动加载类 __autoload()函数

    很多开发者写面向对象的应用程序时,对每个类的定义建立一个 PHP 源文件.一个很大的烦恼是不得不在每个脚本(每个类一个文件)开头写一个长长的包含文件的列表. 在软件开发的系统中,不可能把所有的类都写在 ...

  5. 速战速决 (5) - PHP: 动态地创建属性和方法, 对象的复制, 对象的比较, 加载指定的文件, 自动加载类文件, 命名空间

    [源码下载] 速战速决 (5) - PHP: 动态地创建属性和方法, 对象的复制, 对象的比较, 加载指定的文件, 自动加载类文件, 命名空间 作者:webabcd 介绍速战速决 之 PHP 动态地创 ...

  6. 【php】命名空间 和 自动加载的关系

    目的 本文的目的主要是说明 命名空间的 use 关键词 和 new ClassName 这两个步骤,哪个步骤才会执行自动加载,这是逻辑有点混乱的表现,这种想法也是很正常的,让我们来解密吧 命名空间(n ...

  7. PHP 自动加载类

    类的自动加载 (Autoloading Classes) 在编写面向对象(OOP) 程序时,很多开发者为每个类新建一个 PHP 文件. 这会带来一个烦恼:每个脚本的开头,都需要包含(include)一 ...

  8. 自动加载类PHP中spl_autoload_register函数的用法

    spl_autoload_register(PHP 5 >= 5.1.2) spl_autoload_register — 注册__autoload()函数 说明bool spl_autoloa ...

  9. php中自动加载类_autoload()和spl_autoload_register()实例详解

    一._autoload 自动加载类:当我们实例化一个未定义的类时,就会触此函数.到了php7.1以后版本不支持此函数好像抛弃了 新建一个类文件名字自己随便去:news类在auto.php文件里面去实例 ...

随机推荐

  1. java中int i 会出现i+1i吗

    Java中int是32,范围是-2147483648到2147483647 所以i+1 < i 或者 i-1 > i是会出现的. int i=(int) Math.pow(2, 32); ...

  2. 阿里云上如何利用yum安装jenkins

    一. 安装jdk 确保安装jenkins前jdk已经安装,如何安装见<如何在阿里云上部署war包到tomcat服务器> 二. 安装jenkins 使用以下命令安装jenkins: wget ...

  3. SimpleDateFormat函数语法

    SimpleDateFormat函数语法:         G 年代标志符         y 年         M 月         d 日         h 时 在上午或下午 (1~12)  ...

  4. webpack相关文章

    0.https://doc.webpack-china.org/concepts/ (webpack中文文档) 1.https://segmentfault.com/a/119000000617877 ...

  5. 【scala】异常处理

    Scala 的异常处理和其它语言比如 Java 类似. 抛出异常 Scala 抛出异常的方法和 Java一样,使用 throw 方法 throw new IllegalArgumentExceptio ...

  6. canvas - 饼状图

    <!DOCTYPE html> <html> <head> <title>Canvas测试</title> <meta charset ...

  7. linux initcall机制

    Linux系统启动过程很复杂,因为它既需要支持模块静态加载机制也要支持动态加载机制.模块动态加载机制给系统提供了极大的灵活性,驱动程序既可支持静态编译进内核,也可以支持动态加载机制.Linux系统中对 ...

  8. C++复制构造函数的实现

    复制构造函数是一种特殊的构造函数,有一般构造函数的特性.它的功能是用一个已知的对象来初始化一个被创建的同类对象.复制构造函数的参数传递方式必须按引用来进行传递,请看实例: #include <i ...

  9. js 获取服务器当前时间

    //获取服务器时间 function getNowDate(){ var xhr = null; if(window.XMLHttpRequest){ xhr = new window.XMLHttp ...

  10. (六)java数据类型

      数据类型:决定了变量占据多大的空间,决定了变量存储什么类型的数据 整形: byte     1个字节 short    2个字节 int        4个字节 long     8个字节 浮点型 ...