php_task2

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);
?>

運(yùn)行結(jié)果

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,135評論 0 23
  • 光陰,請別用記憶 去裝扮離別 就像無法用落花 去拼湊燦爛的時(shí)節(jié) 于是我認(rèn)真的準(zhǔn)備告別 在每個(gè)可能離開的渡口 寫下相...
    暖洋洋的小蝸牛閱讀 260評論 0 2
  • 唐瓔曦,今年大二,被大家稱之為“預(yù)言仙女”。 只因那時(shí)我對一個(gè)年年考試掛科的大四學(xué)姐說了句:“你今年肯定不掛科!”...
    瓔曦閱讀 242評論 0 1
  • 我總會羨慕別人的生活。穩(wěn)定的家庭,相愛的戀人,蒸蒸日上的事業(yè),或是情同手足的朋友。在他困難時(shí),會有人為他伸出援助之...
    進(jìn)擊的鹵蛋閱讀 129評論 0 0

友情鏈接更多精彩內(nèi)容