php_task2

作者: 孙宏博 | 来源:发表于2017-10-09 01:39 被阅读0次

people.php

<?php
namespace MyProject;
class People
{
    private $name;
    private $age;
    private $height;
    private $weight;
    private $hobby;
    public static $currentNumber = 0;
    const MAXNUMBER = 2;
    /**
     * People constructor.
     * @param string $name
     * @param int $age
     */
    public function __construct(string $name, int $age, $height, $weight,  $hobby)
    {
        $this->name = $name;
        $this->age = $age;
        $this->height = $height;
        $this->weight= $weight;
        $this->hobby= $hobby;
        self::$currentNumber++ ;
        if( self::$currentNumber > self::MAXNUMBER )
        {
            echo "current people number " .self::$currentNumber."</br>\n";
            self::$currentNumber-- ;
            echo "construct failes        " ;
            echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp    ";
            $this->__destruct();
            return 1;
        }
        echo "current people number " .self::$currentNumber."</br>\n";
        echo "the person's name  is $this->name,</br> 
       the age is  $this->age years old,</br> 
       the height is  $this->height,</br>
       the weight is  $this->weight,</br>
       the hobby is </br>\n";
       var_dump($hobby);
       echo "</br>";
    }
    public function speak($action){
        echo "the method is $action  ";
    }
    public function eat(){
        $this->speak("eat");
        $this->weight=$this->weight+1;
        echo "current weight is ".$this->weight."</br>\n";
    }
    public function sleep(){
    $this->speak("sleep");
    $this->height=$this->height+1;
    echo "current height is ".$this->height."</br>\n";
    }
    public function run(){
        $this->speak("run");
        $this->weight=$this->weight-1;
        echo "current weight is ".$this->weight."</br>\n";
    }
    public function swim(){
        $this->speak("swim");
        $this->weight=$this->weight-1;
        echo "current weight is ".$this->weight."</br>\n";
    }
    public function haveBirthday(){
        $this->speak("haveBirthday");
        $this->age=$this->age+1;
        echo "current age is ".$this->age."</br>\n";
    }
    public function __destruct(){
        echo "destruct of " . __CLASS__ . " " . __METHOD__ . "</br>\n";
    }
}?>

 

main.php

<?php
require "people.php";
use MyProject\People;
$hooby = array(
    0=>"play basketball",
    1 => "play football",
);
$people = new People("ieso1",22,170,110,$hooby);
$people->haveBirthday();
$people->eat();
$people->sleep();
$people->swim();
$people->run();
echo "</br>\n";
$people1 = new People("ieso2",23,180,120,$hooby);
echo "</br>\n";
$people2 = new People("ieso3",24,185,130,$hooby);
?>

运行结果

current people number 1
the person's name is ieso1,
the age is 22 years old,
the height is 170,
the weight is 110,
the hobby is
array(2) { [0]=> string(15) "play basketball" [1]=> string(13) "play football" }
the method is haveBirthday current age is 23
the method is eat current weight is 111
the method is sleep current height is 171
the method is swim current weight is 110
the method is run current weight is 109

current people number 2
the person's name is ieso2,
the age is 23 years old,
the height is 180,
the weight is 120,
the hobby is
array(2) { [0]=> string(15) "play basketball" [1]=> string(13) "play football" }

current people number 3
construct failes destruct of MyProject\People MyProject\People::__destruct
destruct of MyProject\People MyProject\People::__destruct
destruct of MyProject\People MyProject\People::__destruct
destruct of MyProject\People MyProject\People::__destruct

相关文章

  • php_task2

    people.php main.php 运行结果 current people number 1the perso...

  • php_task2(modified)

    任务: 定义如下类People属性: name (string nonstatic) age (int nonst...

网友评论

      本文标题:php_task2

      本文链接:https://www.haomeiwen.com/subject/uxhuyxtx.html