PHP命名空间
来源:互联网 发布:软件测试就业好不好 编辑:程序博客网 时间:2024/06/07 12:36
命名空间
命名空间是抽象的容器,创建它是为了容纳对象名称的逻辑群组。在其他语言中这是为人熟知的功能,而且有时呈现为包或者模块。脚本每天越来越大,也越来越复杂,这使得发明新的标识符难上加难。使用use语句可以讲命名空间导入局部命名空间,而且可以使用更方便的名称作为它的别名。如果文件中有多个命名空间,必须使用大括号括起来。__NAMESPACE__当前命名空间的名称。
animals.php
<?phpnamespace animals\wild{ class animal{ static function whereami(){ print "<br>".__NAMESPACE__; } function __construct(){ $this->type='tiger'; } function get_type(){ return ($this->type); } }}namespace animals\domestic{ class animal { function __construct() { $this->type = 'dog'; } function get_type(){ return $this->type; } }}?>
index.php
<?phprequire_once('animals.php');use \animals\wild\animal as beast;$c = new beast();printf("%s\n",$c->get_type());beast::whereami();?>
显示结果
命名空间和自动加载
__autoload函数,它用来将类自动加载到程序中,有助于自动化require_once指令
function __autoload($class){ print "$class"; exit(0);}
将index.php代码修改如下
<?phpfunction __autoload($class){ print "$class"; exit(0);}//require_once('animals.php');use \animals\wild\animal as beast;$c = new beast();printf("%s\n",$c->get_type());beast::whereami();?>
显示结果
为了借助自动加载使用命名空间,应该制定一个目录层次结构,用斜杠字符替换反斜杠,包括文件。替换字符可以通过str_replace或者preg_replace函数做到,简单的任务,使用str_replace函数比使用preg_replace更省事
【加拿大】Peter MacIntyre Brian Danchilla 【美】Miaden Gogala . PHP编程实战
0 0
- PHP手册-命名空间
- PHP手册-命名空间
- PHP的命名空间
- php命名空间 namespace
- PHP的命名空间
- php 命名空间
- PHP命名空间
- php 命名空间1
- php命名空间2
- PHP中的命名空间
- php 命名空间
- PHP 命名空间 入门
- PHP命名空间
- php命名空间
- PHP中的命名空间
- php namespace-命名空间
- PHP中的命名空间
- PHP的命名空间
- 2017/01/20
- 利用mongod启动mongo
- Centos 安装nginx
- 96. Unique Binary Search Trees
- Hdu1560 DNA sequence(IDA*)
- PHP命名空间
- MySQL的timestamp
- L0phtCrack Password Auditor 7.0.14 x86/x64
- 给出一个包含n个整数的数列,问整数a在数列中的第一次出现是第几个。
- 开通CSDN博客目的:
- java 用边读边下载文件,可用于大数据excel下载
- 1050. String Subtraction (20) PAT甲级
- 【Spring】Spring Framework Reference Documentation中文版8
- 网络编程15最后