php作业01

来源:互联网 发布:微屏软件 编辑:程序博客网 时间:2024/05/19 01:31
<?phpnamespace defpp;class People{    public $name;    public $age;    public $height;    public $weight;    public $hobby;    public static $crtnum;    const maxnum=10;    function __construct(        string $name,int $age,float $height,        float $weight,string $hobby)    {        $this->name=$name;        $this->age=$age;        $this->height=$height;        $this->weight=$weight;        $this->hobby=$hobby;        $this->speak(__METHOD__);        self::$crtnum++;        echo "a man has been born\n";    }    function __destruct()    {        self::$crtnum--;        $this->speak(__METHOD__);        echo "a man die";    }    function speak(string $s)    {        echo $s."\n";    }    function eat()    {        $this->weight++;        $this->speak(__METHOD__);    }    function havebirthday()    {        $this->age++;        $this->speak(__METHOD__);    }    function sleeping()    {        $this->weight++;        $this->speak(__METHOD__);    }    function walk()    {        $this->weight--;        $this->speak(__METHOD__);    }    function run()    {        $this->weight--;        $this->speak(__METHOD__);    }    function swim()    {    $this->weight--;    $this->speak(__METHOD__);    }}?>
<?phpnamespace func_create;require_once("file1.php");use defpp\People;function create(string $name,int $age,float $height,float $weight,string $hobby){    if(People::$crtnum<People::maxnum)    {        $X=new People($name,$age,$height,$weight,$hobby);        return $X;    }    else    {        echo "There are too many people";        return null;    }}?>
<?phpnamespace func;$a1=array("apple","banana","orange");function foo(){    global $a1;    var_dump($a1);}?>``````main.php<?phprequire_once("file1.php");require_once("file2.php");require_once("file3.php");use defpp\People as People;use function func_create\create as create;use function func\foo as foo;foo();var_dump($a1);$fengtiao=create("fengtiao",18,150.0,70.0,"attackothers");$fengtiao->sleeping();$fengtiao->run();$fengtiao->eat();$fengtiao->swim();?>

运行结果

原创粉丝点击