继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

面向对象 (三)命名空间

一句话儿的事
关注TA
已关注
手记 12
粉丝 1
获赞 1
命名空间

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

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

规则限制:
    当前脚本命名空间前不能有任何代码
    一个脚本可以创建多个命名空间
    同一个命名空间下,不能重复声明类,同一个命名空间可以使不同文件,通过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/>";
        }
    }
}
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP