您的位置:首页 > 编程语言 > PHP开发

php 全局类介绍

2017-06-04 20:50 106 查看

上一篇博客 简单的 介绍了一下 命名空间的用法 今天就来给大家 简单的 介绍一下 php的全局类

全局类 名字好像很高大上 其实理解起来也非常的容易 全局类 其实就是 没有加命名空间的类

就像下面的 hello类

<?php

class hello
{
function say()
{
echo "hello world !";
}
}


调用全局类也非常容易 像这样就可以很容易的调用

<?php

require_once('test1.php');
require_once('test2.php');
require_once('hello.php');
use space\test1\test;
use space\test2\test as test2;

4000
$test1 = new test();
$test2 = new test2();
$hello = new \hello();
$test1->say();
$test2->say();
$hello->say();


在 new 关键字 实例化类前 在类名前加 \ 就可以实例化全局类

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息