PHP中命名空间(namespace)的使用方法
2018-03-24 19:36
591 查看
PHP中的命名空间一是为了解决类名、函数名、常量名的冲突问题;二是为了很长的标识符名称创建一个别名,提高代码的可读性。
下面介绍几种命名空间的使用方法,首先展示一下我的文件目录结构以及a.php 和 b.php 代码
a.php<?php
namespace modelA;
header("Content-type:text/html;charset=utf-8");
class a{
static function out(){
echo "这是modelA模块中的A文件";
}
}
?>b.php<?php
namespace modelB;
header("Content-type:text/html;charset=utf-8");
class a{
function out(){
echo "这是modelB模块中的A文件";
}
}第一种使用方法:
c.php<?php
header("Content-type:text/html;charset=utf-8");
include_once "./modelA/a.php";
include_once "./modelB/a.php";
use modelA\a;
use modelB\a as b;
a::out();
echo "<br/>";
$b = new b();
$b->out();
在这种方法中,需要使用iniclude_once()方法引入文件,也需要使用命名空间。可能有些人会认为:不是已经使用use modelA\a了吗,那就应该把文件引入进来了,为什么还要include_once()一下呢?那是因为use modelA\a仅仅是声明要使用modelA命名空间下的a文件,可以理解为逻辑上的使用,为了防止重名。而include_once()则是物理上的使用,将代码导入到c.php中。use 和 include缺一不可。
可是在一些框架中,要使用另一个类文件时,我们只需要use modelA\a就可以了,不再需要include文件了,这是因为这些框架中一般都封装了自动加载机制,一般使用魔术方法__autoload()或spl_autoload_register()方法。
__autoload()方法在什么时候触发呢,当代码中使用new关键字实例化一个对象时,如果该类没有在当前php文件中被定义,将会触发__autoload函数,此时,就可以引进定义该类的php文件,这样就能成功实例化一个类了。另外,如果需要include 100个PHP文件,那么代码将会非常冗杂,文件也会非常庞大,而使用该方法则会根据命名空间动态加载类,很好的解决引入文件过多的问题。
__autoload()使用方法:<?php
header("Content-type:text/html;charset=utf-8");
use modelA\a;
use modelB\a as b;
function __autoload($classname){
$classpath = "{$classname}.php";
if(file_exists($classpath))
include_once($classpath);
else
echo $classpath . "not found";
}
a::out();
echo "<br/>";
$b = new b();
$b->out();
其实__spl_autoload_register()方法的使用和__autoload()的使用几乎一样,只是前者可以自定义函数替代__autoload(),使用如下:<?php
header("Content-type:text/html;charset=utf-8");
use modelA\a;
use modelB\a as b;
function diy($classname){
$classpath = "{$classname}.php";
if(file_exists($classpath))
include_once($classpath);
else
echo $classpath . "not found";
}
spl_autoload_register("diy");
a::out();
echo "<br/>";
$b = new b();
$b->out();
下面介绍几种命名空间的使用方法,首先展示一下我的文件目录结构以及a.php 和 b.php 代码
a.php<?php
namespace modelA;
header("Content-type:text/html;charset=utf-8");
class a{
static function out(){
echo "这是modelA模块中的A文件";
}
}
?>b.php<?php
namespace modelB;
header("Content-type:text/html;charset=utf-8");
class a{
function out(){
echo "这是modelB模块中的A文件";
}
}第一种使用方法:
c.php<?php
header("Content-type:text/html;charset=utf-8");
include_once "./modelA/a.php";
include_once "./modelB/a.php";
use modelA\a;
use modelB\a as b;
a::out();
echo "<br/>";
$b = new b();
$b->out();
在这种方法中,需要使用iniclude_once()方法引入文件,也需要使用命名空间。可能有些人会认为:不是已经使用use modelA\a了吗,那就应该把文件引入进来了,为什么还要include_once()一下呢?那是因为use modelA\a仅仅是声明要使用modelA命名空间下的a文件,可以理解为逻辑上的使用,为了防止重名。而include_once()则是物理上的使用,将代码导入到c.php中。use 和 include缺一不可。
可是在一些框架中,要使用另一个类文件时,我们只需要use modelA\a就可以了,不再需要include文件了,这是因为这些框架中一般都封装了自动加载机制,一般使用魔术方法__autoload()或spl_autoload_register()方法。
__autoload()方法在什么时候触发呢,当代码中使用new关键字实例化一个对象时,如果该类没有在当前php文件中被定义,将会触发__autoload函数,此时,就可以引进定义该类的php文件,这样就能成功实例化一个类了。另外,如果需要include 100个PHP文件,那么代码将会非常冗杂,文件也会非常庞大,而使用该方法则会根据命名空间动态加载类,很好的解决引入文件过多的问题。
__autoload()使用方法:<?php
header("Content-type:text/html;charset=utf-8");
use modelA\a;
use modelB\a as b;
function __autoload($classname){
$classpath = "{$classname}.php";
if(file_exists($classpath))
include_once($classpath);
else
echo $classpath . "not found";
}
a::out();
echo "<br/>";
$b = new b();
$b->out();
其实__spl_autoload_register()方法的使用和__autoload()的使用几乎一样,只是前者可以自定义函数替代__autoload(),使用如下:<?php
header("Content-type:text/html;charset=utf-8");
use modelA\a;
use modelB\a as b;
function diy($classname){
$classpath = "{$classname}.php";
if(file_exists($classpath))
include_once($classpath);
else
echo $classpath . "not found";
}
spl_autoload_register("diy");
a::out();
echo "<br/>";
$b = new b();
$b->out();
相关文章推荐
- PHP命名空间(namespace)的使用基础及示例
- PHP中命名空间的创建-使用namespace
- PHP命名空间(namespace)的使用基础及示例
- PHP中的命名空间(namespace)及其使用详解
- PHP命名空间(namespace)的动态访问及使用技巧
- PHP命名空间(Namespace)的使用详解
- PHP命名空间(Namespace)的使用详解
- PHP命名空间(Namespace)的使用详解
- PHP中的命名空间(namespace)及其使用详解
- PHP 5.3中的命名空间使用方法浅述
- PHP命名空间(Namespace)的使用详解
- PHP命名空间(Namespace)的使用详解
- PHP命名空间(Namespace)的使用详解(转)
- PHP命名空间(Namespace)的使用详解
- PHP命名空间(Namespace)的使用详解
- PHP 使用命名空间(namespace),实现自动加载
- PHP命名空间(Namespace)的使用详解
- php命名空间(namespace)内如何使用系统类
- PHP命名空间(Namespace)的使用详解
- PHP 5.3中的命名空间使用方法