5月8日作业
命名空间 namespace use
代码 ; factory.php
实例
<?php namespace Factory; //namespace School; //use School; include('School.php'); use School; define("MADE", "制作"); function made() { echo '我是制作汽车的'; } //function learn() //{ // echo '我是学习中文的'; //} class Factory { const WORKER = '张鹏'; public $name = '王虎'; public function work() { echo '我是制作飞机的'; } } echo MADE; echo '<hr>'; made(); echo '<hr>'; echo Factory::WORKER; echo '<br>'; $obj = new Factory(); echo $obj->name; echo '<br>'; $obj -> work(); echo '<hr color="red" size=3>'; //use School; echo STUDY; echo '<hr>'; learn(); echo '<hr>'; $obj1 = new School(); echo $obj1->name; echo '<br>'; $obj1 -> study(); echo '<hr>';
运行实例 »
点击 "运行实例" 按钮查看在线实例
代码:School.php
实例
<?php namespace School; define("STUDY", "html"); function learn() { echo '我是学习数学'; } class School { const STUDY = '刘丽丽'; public $name = '王秀秀'; public function study() { echo '我是学习 php '; } } //echo STUDY; //echo '<hr>'; // //learn(); //echo '<hr>'; // //echo School::STUDY; //echo '<br>'; //$obj = new School(); //echo $obj->name; //echo '<br>'; //$obj -> study(); //echo '<hr>';
运行实例 »
点击 "运行实例" 按钮查看在线实例
代码运行图片: