--- tags: Laravel --- # 物件使用 ### ==物件建立== * 我們可以建立一個物件的概念、例如建立一個車子的骨架跟顏色等 ```php= class Cart { public $man = 90; public function getMan() { return $this->man; } public function eat($foot) { //this 為使用物件 {} 所有內容 $this->man += $foot; return $this->man; } } ``` ### ==使用物件== * 建立物件後我們即可使用物件所有的內容 ```php= //繼承使用 Cart 所有的屬性與內容加以使用 $newCart = new Cart; echo ($newCart->getMan() . "\r\n"); echo ($newCart->eat(80)); //顯示 90 170 ``` ### ==繼承物件== * 當我們要繼承上一個物件時、可在寫新的class 使用extends去繼承上個calss ```php class 新的名稱 extends 繼承class的名稱 { } ``` * 在使用new 來使用新的所有物件 ```php $new = new MayCart; ``` > 完整範例使用 ```php= //原有的物件 class Cart { public $man = 90; public function getMan() { return $this->man; } public function eat($foot) { $this->man += $foot; return $this->man; } } // 繼承原有的物件使用 class MayCart extends Cart { public $man = 50; } //繼承後 父元素所有屬性都可以使用 $new = new MayCart; echo ($new->getMan() . "\r\n"); //顯示50 echo ($new->eat(50) . "\r\n"); //顯示100 ```