当前位置: 首页 > news >正文

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


本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/5171215.html,如需转载请自行联系原作者

相关文章:

  • Lucene的各中文分词比较
  • altiium designer改变图纸大小
  • 自动化Oracle数据库静默安装
  • 数字1的数量
  • 消息中间件的应用——谈谈秒杀(转)
  • 买二手房不如买新房
  • 基本数据结构——栈
  • JAVA入门[2]-安装Maven
  • 2017《Java技术》预备作业02
  • makefile 判断 64bit or 32 bit
  • Hadoop学习笔记一
  • 转入墙内:SAS HBA crossflashing or flashing to IT mode, Dell Perc H200 and H310
  • 循环引用问题
  • ZooKeeper 在硬盘满后,无法再次启动,抛出Last transaction was partial 解决方法
  • 高性能业务架构解决方案(Keepalive+MySQL)
  • (十五)java多线程之并发集合ArrayBlockingQueue
  • 10个最佳ES6特性 ES7与ES8的特性
  • axios请求、和返回数据拦截,统一请求报错提示_012
  • C++入门教程(10):for 语句
  • CSS 三角实现
  • gitlab-ci配置详解(一)
  • Javascripit类型转换比较那点事儿,双等号(==)
  • JavaScript对象详解
  • JavaSE小实践1:Java爬取斗图网站的所有表情包
  • Java反射-动态类加载和重新加载
  • JAVA之继承和多态
  • Python_网络编程
  • SQL 难点解决:记录的引用
  • Webpack 4 学习01(基础配置)
  • 不上全站https的网站你们就等着被恶心死吧
  • 海量大数据大屏分析展示一步到位:DataWorks数据服务+MaxCompute Lightning对接DataV最佳实践...
  • 前端面试之CSS3新特性
  • 区块链技术特点之去中心化特性
  • 使用putty远程连接linux
  • 以太坊客户端Geth命令参数详解
  • 终端用户监控:真实用户监控还是模拟监控?
  • 大数据全解:定义、价值及挑战
  • #{} 和 ${}区别
  • $L^p$ 调和函数恒为零
  • (2)nginx 安装、启停
  • (solr系列:一)使用tomcat部署solr服务
  • (黑马C++)L06 重载与继承
  • (三分钟)速览传统边缘检测算子
  • (算法)求1到1亿间的质数或素数
  • * 论文笔记 【Wide Deep Learning for Recommender Systems】
  • .NET 中的轻量级线程安全
  • .net6+aspose.words导出word并转pdf
  • .NET开发不可不知、不可不用的辅助类(一)
  • .net通用权限框架B/S (三)--MODEL层(2)
  • .Net小白的大学四年,内含面经
  • [.net 面向对象程序设计进阶] (19) 异步(Asynchronous) 使用异步创建快速响应和可伸缩性的应用程序...
  • [20170728]oracle保留字.txt
  • [BUUCTF]-PWN:wustctf2020_number_game解析(补码,整数漏洞)
  • [C/C++]关于C++11中的std::move和std::forward
  • [CLR via C#]11. 事件