php學習筆記(一)————php類的概念

來源:互聯網
上載者:User

標籤:eth   else   效果   get   image   分享   使用   屬性   基本   

<?php//類的概念/* * 一個類包含自己的屬性和函數 *  * 屬性:屬於類自己的常量和變數 *  * 方法:就是函數 *  * 類是一類事物的抽象 *///例子://車就是一種抽象class Car{    //車的基本屬性:有輪子,有顏色,可以拉貨(人)        public $color = ‘red‘;//預設紅色    public $wheel_size = 4;//預設4個        //方法:拉貨預設    public function pull_some_thing($something = ‘貨‘){        echo "我是一輛 $this->color 色的,有 $this->wheel_size 個輪子的車,我正在拉 $something ";    }}$qiche  = new Car();$qiche->color = ‘黑‘;$qiche->wheel_size = 12;$qiche->pull_some_thing(‘人‘);echo ‘<hr/>‘;//例子2:class A{    function foo()    {        if (isset($this)) {            echo ‘$this is defined (‘;            echo get_class($this);//獲得這個$this 代表的類            echo ")\n";        } else {            echo "\$this is not defined.\n";        }    }}class B{    function bar()    {        // Note: the next line will issue a warning if E_STRICT is enabled.        A::foo();//在b類下調用A類的foo方法,獲得的$this 是B類    }}$a = new A();$a->foo();@A::foo();//直接在類外調用A類的foo方法不能擷取到A類(就是說不能在類外使用$this指代目標類)//$this->foo();//會報錯$b = new B();@$b->bar();// Note: the next line will issue a warning if E_STRICT is enabled.@B::bar();echo ‘<br/>‘;

執行效果 

php學習筆記(一)————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.