PHP類相關

來源:互聯網
上載者:User

標籤:ogr   架構   str   class   fun   php物件導向   elf   zha   計算   

 PHP物件導向OOP(Object Oriented Programming),是一種電腦編程架構。

  首先,我們要理解的是PHP物件導向中的的類class,對象以及建構函式與解構函式。

1.類class

class Student{    //1.成員屬性    /*     * <1>.public關鍵字表示聲明的變數是共用的,任何地方都能訪問到*/    public $name = "zhangsan",$score,$num;    //建構函式傳參    public function __construct($name,$score,$num)    {        $this->name = $name;        $this->score = $score;        $this->num = $num;    }    //解構函式    public  function __destruct()    {        echo "函數死了";        // TODO: Implement __destruct() method.    }    //2.成員方法    //當前學生會唱歌    public function sing($songname){        return"會唱{$songname}歌";    }    //當前學生會跑步    public function run(){        return"學生會跑步";    }    //介紹自己    public function showSelf(){      echo "我叫{$this->name},學號是{$this->num},我考了{$this->score}分,我{$this->sing("小星星")}";    }}

2.對象,是通過類class執行個體化new關鍵字出來的,他可以使用類裡面的屬性和方法,如下

//執行個體化對象$studeng1 = new  Student("王五",95,1001);//對象訪問成員屬性$studeng1->name="lisi";//對象訪問成員方法$studeng1->sing("小星星");$studeng2->showSelf();

類是一個範圍,對象是類裡面的一個個體。

 

PHP類相關

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.