php中namespace使用过程

//test2.php
<?php
    namespace Pj\Test;
    function test($val){
        if(is_numeric($val)){
            return $val+12;
        }
        die("参数不是数字!");
    };
 ?>
//test1.php
<?php
    header('Access-Control-Allow-Origin:*');

    require_once dirname(__FILE__)."/test2.php";//1

    use \Pj\Test as NowSpace;//2

    $result = NowSpace\test(23);//3 
    echo $result;
    
 ?>

test1.php和test2.php父目录相同

1.require引入文件

2.导入命名空间(可用as定义代替该空间)

3.使用空间下的代码

posted on 2016-12-20 11:15  LSMORPJER  阅读(169)  评论(0)    收藏  举报

导航