Blogger Information
Blog 12
fans 0
comment 1
visits 5842
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
8.5【自动加载与多级命名空间的用法】
小陈先生的博客
Original
725 people have browsed it

实例

<?php

namespace zuoye;
//创建一个类, 它的命名空间必须它所在绝对路径完全一致
class zuoye1{
    public static function a(){
        return __DIR__."/".__CLASS__.".php";
    }
}
echo zuoye1::a(). '<br>';


//使用spl_autoload_register()实现它的自动加载...., 必须要用到多级空间结构名称
class zuoye2{
    public static function autoLoader(){
        spl_autoload_register(function ($className) {
            $path =  __DIR__ . '/' . $className . '.php';
            //echo $path;
            if (file_exists($path)) {
                require $path;
            }
        });
    }
}
zuoye2::autoLoader();
use \test1\Test1 as a;
use \test2\Test2 as b;
//先判断下a是否存在
echo class_exists(a::class)?a::class.'类存在':'类不存在';
echo a::get();
echo b::get();

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:关于as 关键字, 用得不多, 但却很有用
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post