zoukankan      html  css  js  c++  java
  • PHP命名空间

    这几天没更,是因为上一篇翻译太长了,233333,其实并没有间断,今天看了下啊PHP命名空间,如下:

    【参考】PHP命名空间(Namespace)的使用详解官方文档菜鸟教程


      命名空间一个最明确的目的就是解决重名问题,PHP中不允许两个函数或者类出现相同的名字,否则会产生一个致命的错误。

    1. 约定一个前缀。

    例:项目中有两个模块:article和message board,它们各自有一个处理用户留言的类Comment。之后我可能想要增加对所有用户留言的一些信息统计功能,比如说我想得到所有留言的数量。这时候调用它们Comment提供的方法是很好的做法,但是同时引入各自的Comment类显然是不行的,代码会出错,在另一个地方重写任何一个Comment也会降低维护性。那这时只能重构类名,我约定了一个命名规则,在类名前面加上模块名,像这样:Article_Comment、MessageBoard_Comment

    可以看到,名字变得很长,那意味着以后使用Comment的时候会写上更多的代码(至少字符多了)。并且,以后如果要对各个模块增加更多的一些整合功能,或者是互相调用,发生重名的时候就需要重构名字。当然在项目开始的时候就注意到这个问题,并规定命名规则就能很好的避免这个问题。

    2. 使用命名空间。

    创建一个命名空间需要使用namespace关键字,要注意的是,当前脚本文件的第一个命名空间前面不能有任何代码如下是错误的:

    <html></html>
    <?php
    namespace Article;
    ?>

    在不同空间之间不可以直接调用其它元素,需要使用命名空间的语法:

    <?php
    namespace Article;
    class Comment { }
    
    namespace MessageBoard;
    class Comment { }
    
    //调用当前空间(MessageBoard)的Comment类
    $comment = new Comment();
    
    //调用Article空间的Comment类
    $article_comment = new ArticleComment();
    ?>

    公共空间
    我有一个common_inc.php脚本文件,里面有一些好用的函数和类:

    <?php
    function getIP() { }
    
    class FilterXSS { }
    ?>

    在一个命名空间里引入这个脚本,脚本里的元素不会归属到这个命名空间。如果这个脚本里没有定义其它命名空间,它的元素就始终处于公共空间中:

    <?php
    namespace BlogArticle;
    
    //引入脚本文件
    include './common_inc.php';
    
    $filter_XSS = new FilterXSS(); //出现致命错误:找不到BlogArticleFilterXSS类
    
    $filter_XSS = new FilterXSS(); //正确
    
    ?>

    调用公共空间的方式是直接在元素名称前加 就可以了,否则PHP解析器会认为我想调用当前空间下的元素。除了自定义的元素,还包括PHP自带的元素,都属于公共空间。

    要提一下,其实公共空间的函数和常量不用加 也可以正常调用(不明白PHP为什么要这样做),但是为了正确区分元素,还是建议调用函数的时候加上

    名称术语

    1. 非限定名称,或不包含前缀的类名称,例如 $comment = new Comment();。如果当前命名空间是BlogArticle,Comment将被解析为BlogArticleComment。如果使用Comment的代码不包含在任何命名空间中的代码(全局空间中),则Comment会被解析为Comment。

    2. 限定名称,或包含前缀的名称,例如 $comment = new ArticleComment();。如果当前的命名空间是Blog,则Comment会被解析为BlogArticleComment。如果使用Comment的代码不包含在任何命名空间中的代码(全局空间中),则Comment会被解析为Comment。

    3. 完全限定名称,或包含了全局前缀操作符的名称,例如 $comment = new ArticleComment();。在这种情况下,Comment总是被解析为代码中的文字名(literal name)ArticleComment。
     

    其实可以把这三种名称类比为文件名(例如 comment.php)、相对路径名(例如 ./article/comment.php)、绝对路径名(例如 /blog/article/comment.php)。

    <?php
     
    //创建空间Blog
    namespace Blog;
    class Comment { }
    
    //非限定名称,表示当前Blog空间
    //这个调用将被解析成 BlogComment();
    $blog_comment = new Comment();
    
    //限定名称,表示相对于Blog空间
    //这个调用将被解析成 BlogArticleComment();
    $article_comment = new ArticleComment(); //类前面没有反斜杆
    
    //完全限定名称,表示绝对于Blog空间
    //这个调用将被解析成 BlogComment();
    $article_comment = new BlogComment(); //类前面有反斜杆
    
    //完全限定名称,表示绝对于Blog空间
    //这个调用将被解析成 BlogArticleComment();
    $article_comment = new BlogArticleComment(); //类前面有反斜杆
    
    //创建Blog的子空间Article
    namespace BlogArticle;
    class Comment { }
    
    ?>

    别名和导入
    别名和导入可以看作是调用命名空间元素的一种快捷方式。PHP并不支持导入函数或常量。

    它们都是通过使用use操作符来实现:

    <?php
    namespace BlogArticle;
    class Comment { }
    
    //创建一个BBS空间(我有打算开个论坛)
    namespace BBS;
    
    //导入一个命名空间
    use BlogArticle;
    //导入命名空间后可使用限定名称调用元素
    $article_comment = new ArticleComment();
    
    //为命名空间使用别名
    use BlogArticle as Arte;
    //使用别名代替空间名
    $article_comment = new ArteComment();
    
    //导入一个类
    use BlogArticleComment;
    //导入类后可使用非限定名称调用元素
    $article_comment = new Comment();
    
    //为类使用别名
    use BlogArticleComment as Comt;
    //使用别名代替空间名
    $article_comment = new Comt();
    ?>

    我注意到,如果导入元素的时候,当前空间有相同的名字元素将会怎样?显然结果会发生致命错误。

    <?php
    namespace BlogArticle;
    class Comment { }
    
    namespace BBS;
    class Comment { }
    Class Comt { }
    
    //导入一个类
    use BlogArticleComment;
    $article_comment = new Comment(); //与当前空间的Comment发生冲突,程序产生致命错误
    
    //为类使用别名
    use BlogArticleComment as Comt;
    $article_comment = new Comt(); //与当前空间的Comt发生冲突,程序产生致命错误
    ?>

     如下也是体现命名空间的引用方式:

    file1.php 文件代码

    //file1.php 文件代码
    
    <?php
    
    namespace FooBarsubnamespace; 
    const FOO = 1;
    function foo() {}
    class foo
    {
        static function staticmethod() {}
    }
    ?>

    file2.php 文件代码

    <?php
    namespace FooBar;
    include 'file1.php';
    
    const FOO = 2;
    function foo() {}
    class foo
    {
        static function staticmethod() {}
    }
    
    /* 非限定名称 */
    foo(); // 解析为 FooBarfoo resolves to function FooBarfoo
    foo::staticmethod(); // 解析为类 FooBarfoo的静态方法staticmethod。resolves to class FooBarfoo, method staticmethod
    echo FOO; // resolves to constant FooBarFOO
    
    /* 限定名称 */
    subnamespacefoo(); // 解析为函数 FooBarsubnamespacefoo
    subnamespacefoo::staticmethod(); // 解析为类 FooBarsubnamespacefoo,
                                      // 以及类的方法 staticmethod
    echo subnamespaceFOO; // 解析为常量 FooBarsubnamespaceFOO
                                      
    /* 完全限定名称 */
    FooBarfoo(); // 解析为函数 FooBarfoo
    FooBarfoo::staticmethod(); // 解析为类 FooBarfoo, 以及类的方法 staticmethod
    echo FooBarFOO; // 解析为常量 FooBarFOO
    ?>
  • 相关阅读:
    动画编程1
    驾照助手
    《机器学习进阶》Udacity 机器学习基础 决策树+迷你项目
    《机器学习进阶》Udacity 机器学习基础 异常值+交叉验证
    《机器学习进阶》Udacity 机器学习基础 评估指标
    《机器学习进阶》Udacity 监督学习 决策树
    《机器学习进阶》Udacity 监督学习 SVM支持向量机
    入门模拟(图形输出)——B1036.跟奥巴马一起编程( 注意行数为偶数或奇数的情况)
    入门模拟——A1036.Boys vs Girls
    leetcode 深搜广搜
  • 原文地址:https://www.cnblogs.com/kuboy/p/7772671.html
Copyright © 2011-2022 走看看