PHP 面向对象 (三)命名空间

命名空间

作用:隔离代码,避免命名冲突

  • 命名空间分类:

    1. 非限定命名空间(类比为 当前路径)
    2. 限定命名空间(类比为 相对路径)
    3. 完全限定命名空间(类比为 绝对路径)
  • 规则限制:

    1. 当前脚本命名空间前不能有任何代码
    2. 一个脚本可以创建多个命名空间
    3. 同一个命名空间下,不能重复声明类,同一个命名空间可以使不同文件,通过require引入即可
  • 引入类

    1. use引入
    2. 5.6支持引入全局常量和函数

别名作用: 别名用于避免冲突

实践

<?php
namespace Article {
    require("./demo4.php"); //引入其他文件
    use MessageBoard\Comment as MessageComment; //别名
    use function MessageBoard\test; //引入函数
    class Comment
    {
        public $title;
        public $content;
        public function __construct($title, $content)
        {
            $this->title = $title;
            $this->content = $content;
        }

        public function getInfo()
        {
            echo $this->title.'--'.$this->content;echo "<br/>";
            $message_comment = new MessageComment('use_message_comment_title', 'use_message_comment_content');
            echo "<br/>";
            echo '通过use引入'.$message_comment->title;echo "<br/>";
            echo MessageComment::COMMENT;echo "<br/>";
            echo APP;echo "<br/>";
            test();echo "<br/>";
        }
    }
}

namespace MessageBoard {
    define('APP', '常量MessageBoard');
    function test(){
        echo 'this is test';
    }
    class Comment
    {
        const COMMENT = '常量COMMENT';
        public $title;
        public $content;
        public function __construct($title, $content)
        {
            $this->title = $title;
            $this->content = $content;
        }

        public function getInfo()
        {
            echo $this->title.'--'.$this->content;echo "<br/>";
        }
    }

    //当前路径(非限定名称)
    $messageComment = new Comment('message_title','message_content');
    $messageComment->getInfo();
    //绝对路径(完全限定名称)
    $articleConment = new \Article\Comment('article_title', 'article_content');
    $articleConment->getInfo();
    //相对路径(限定名称)
    $articleConment = new Article\Comment('message_article_title', 'message_article_content');
    $articleConment->getInfo();
    //绝对路径(完全限定名称)
    $articleConment1 = new \Article\Comment1('article_title1', 'article_content1');
    $articleConment1->getInfo();
}

namespace MessageBoard\Article {
    class Comment
    {
        public $title;
        public $content;
        public function __construct($title, $content)
        {
            $this->title = $title;
            $this->content = $content;
        }
        public function getInfo()
        {
            echo $this->title.'--'.$this->content;echo "<br/>";
        }
    }
}
demo4.php Article 命名空间内引入的文件
<?php
namespace Article {
    class Comment1
    {
        public $title;
        public $content;
        public function __construct($title, $content)
        {
            $this->title = $title;
            $this->content = $content;
        }

        public function getInfo()
        {
            echo $this->title.'--'.$this->content;echo "<br/>";
        }
    }
}

复制代码,即可运行。 谢谢你的浏览,如有错误,欢迎指正哈!!!

本作品采用《CC 协议》,转载必须注明作者和本文链接
写的不好,就当是整理下思绪吧。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!