亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

面向對象 (三)命名空間

標簽:
PHP
命名空间

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

命名空间分类:
    非限定命名空间(类比为 当前路径)
    限定命名空间(类比为 相对路径)
    完全限定命名空间(类比为 绝对路径)

规则限制:
    当前脚本命名空间前不能有任何代码
    一个脚本可以创建多个命名空间
    同一个命名空间下,不能重复声明类,同一个命名空间可以使不同文件,通过require引入即可
    
引入类
    use引入
    5.6支持引入全局常量和函数

别名
    别名用于避免冲突
实践 demo3.php
<?php
/**
 * Desc: 类规范
 * User: zyy
 * Date: 2020-01-12
 * Time: 下午 13:07
 */
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

<?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/>";
        }
    }
}
點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消