批改状态:未批改
老师批语:
通过今天的学习,我懂得了如何创建命名空间,以及如何引用命名空间及在命名空间中,访问成员变量,以下是我的编程代码:
1,用大括号语法实现在一个脚本中创建多个命名空间并访问成员
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/9/7
* Time: 16:28
*/
namespace test1{
class Demo{
public $name='小孟';
}
const TITLE='PHP中文网';
function add($a,$b)
{
return $a.'+'.$b;
}
}
//声明命名空间test2
namespace test2
{
class Demo1{
public $name='小红';
}
const TITLE='区块链';
function add($a,$b)
{
return $a.'+'.$b;
}
}
namespace
{
echo (\test1\add('1','2'));
}点击 "运行实例" 按钮查看在线实例
2,使用use 导入其它脚本中的类/常量/函数,并使用别名方式访问
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/9/7
* Time: 16:42
*/
namespace demo1;
class test2
{
public $name='小军';
public function show()
{
return 'hello world';
}
}点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号