php_task2

people.php

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."
\n"; self::$currentNumber-- ; echo "construct failes " ; echo "            "; $this->__destruct(); return 1; } echo "current people number " .self::$currentNumber."
\n"; echo "the person's name is $this->name,
the age is $this->age years old,
the height is $this->height,
the weight is $this->weight,
the hobby is
\n"; var_dump($hobby); echo "
"; } 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."
\n"; } public function sleep(){ $this->speak("sleep"); $this->height=$this->height+1; echo "current height is ".$this->height."
\n"; } public function run(){ $this->speak("run"); $this->weight=$this->weight-1; echo "current weight is ".$this->weight."
\n"; } public function swim(){ $this->speak("swim"); $this->weight=$this->weight-1; echo "current weight is ".$this->weight."
\n"; } public function haveBirthday(){ $this->speak("haveBirthday"); $this->age=$this->age+1; echo "current age is ".$this->age."
\n"; } public function __destruct(){ echo "destruct of " . __CLASS__ . " " . __METHOD__ . "
\n"; } }?>

 

main.php

"play basketball",
    1 => "play football",
);
$people = new People("ieso1",22,170,110,$hooby);
$people->haveBirthday();
$people->eat();
$people->sleep();
$people->swim();
$people->run();
echo "
\n"; $people1 = new People("ieso2",23,180,120,$hooby); echo "
\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)