作业1
<?php//file1
namespace Myproject\file1;
function foo(){
echo "this is myproject file1\n";
}
class demo{
const NUMBER=1;
public function show_number(){
echo self::NUMBER."\n";
}
}
<?php//file2
namespace Myproject\file2;
function foo(){
echo "this is myproject file2\n";
}
class demo{
const NUMBER=2;
public function show_number(){
echo self::NUMBER."\n";
}
}
<?php//file3
namespace Myproject\file3;
function foo(){
echo "this is myproject file3\n";
}
class demo{
const NUMBER=3;
public function show_number(){
echo self::NUMBER."\n";
}
}
<?php//main.php
require_once("file1.php");
require_once("file2.php");
require_once("file3.php");
use function Myproject\file1\foo as foo1;
use function Myproject\file2\foo as foo2;
use function Myproject\file3\foo as foo3;
foo1();
foo2();
foo3();
use Myproject\file1\demo as demo1;
use Myproject\file2\demo as demo2;
use Myproject\file3\demo as demo3;
$demo1 = new demo1();
echo demo1::NUMBER."\n";
$demo2 = new demo2();
demo2::show_number();
$demo3 = new demo3();
demo3::show_number();
作业1结果截图
twthomework1.PNG
作业2
<?php//people.php
class People{
private $name;
private $age;
private $height;
private $weight;
private $hobby;
public static $currentNumber=0;
const MAXNUMBER = 10;
public function __construct(string $name,int $age,float $height,float $weight,array $hobby){
if($currentNumber<MAXNUMBER)
{
echo "a people has been created.\n";
echo $name." ".$age." ".$height." ".$weight." ";
foreach ($hobby as $key => $value) {
echo $value." ";
}
echo "\n";
self::$currentNumber++;
}
else
{
echo "cannot create this people.\n";
}
}
public function speak(string $method){
echo $method."\n";
}
public function eat(){
$this->speak(__METHOD__);
$weight++;
}
public function haveBirthday(){
$this->speak(__METHOD__);
$age++;
}
public function sleep(){
$this->speak(__METHOD__);
$height++;
}
public function walk(){
$this->speak(__METHOD__);
$weight--;
}
public function swim(){
$this->speak(__METHOD__);
$weight--;
}
public function run(){
$this->speak(__METHOD__);
$weight--;
}
public function __destruct(){
echo "a people has been killed\n";
$currentNumber--;
}
}
<?php//main.php
require_once("people.php");
$hobby=array("guitar","code");
$person=new people("2huan",18,180.000,90.000,$hobby);
$person->eat();
$person->haveBirthday();
$person->sleep();
$person->walk();
$person->swim();
$person->run();
作业2结果截图
twthomeowrk2.PNG
网友评论