实践 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/>";
}
}
}