栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

面向对象 (三)命名空间

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

面向对象 (三)命名空间

命名空间

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

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

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

别名
    别名用于避免冲突
实践 demo3.php
title = $title;
     $this->content = $content;
 }

 public function getInfo()
 {
     echo $this->title.'--'.$this->content;echo "
"; $message_comment = new MessageComment('use_message_comment_title', 'use_message_comment_content'); echo "
"; echo '通过use引入'.$message_comment->title;echo "
"; echo MessageComment::COMMENT;echo "
"; echo APP;echo "
"; test();echo "
"; } } } 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 "
"; } } //当前路径(非限定名称) $messageComment = new Comment('message_title','message_content'); $messageComment->getInfo(); //绝对路径(完全限定名称) $articleConment = new ArticleComment('article_title', 'article_content'); $articleConment->getInfo(); //相对路径(限定名称) $articleConment = new ArticleComment('message_article_title', 'message_article_content'); $articleConment->getInfo(); //绝对路径(完全限定名称) $articleConment1 = new ArticleComment1('article_title1', 'article_content1'); $articleConment1->getInfo(); } namespace MessageBoardArticle { 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 "
"; } } }
demo4.php

title = $title;
     $this->content = $content;
 }

 public function getInfo()
 {
     echo $this->title.'--'.$this->content;echo "
"; } } }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/226809.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号