php_task1

2017-10-09  本文已影响0人  孙宏博

任务:

创建file1.php, file2.php, file3.php 分别定义不同的命名空间 分别定义⽅法:foo(),定义类Demo,定义常量NUMBER 创建main.php,并在其中引⼊file123,并分别调⽤其中的⽅法,创建类并输出常量。

file1.php

 <?php
namespace task1\File1;
function foo()
{
    echo "file1.php\n";
}
?>

file2.php

<?php
namespace task1\File2;
class Demo{
    private $name;
    public function __construct(){
        $this->name = "class defined in task1\\file2\\Demo";
    }
    public function speak(string $na){
        echo $na. " is an object from ".$this->name . "\n";
    }
}
?>

file3.php

<?php
namespace task1\File3;
const NUMBER = 266;
?>

main.php

<?php
require("./file1.php");
require("./file2.php");
require("./file3.php");
use function task1\File1\foo as foo1;
foo1();
echo "</br>\n";
$demo1=new task1\File2\Demo();
$demo1->speak("demo1");
echo "</br>\n";
use const task1\File3\NUMBER as number1;
echo number1."\n";
echo "</br>\n";
?>

运行结果

task1运行结果.jpg
上一篇下一篇

猜你喜欢

热点阅读