函式

將不同邏輯包裝成一個函式(function)

<?php

    function getWeight(){
        $myWeight = 70;
        $food = 5;
        return $myWeight + $food;
    }

    echo(getWeight());

另還有參數表達方式

此下面例子來說這個參數叫$food,就可以依照這個參數帶入函數去做加減

 <?php
    function getWeight($food){
        $myWeight = 70;
        return $myWeight + $food;
    }

    echo(getWeight(50));

物件概念

讓變數跟方法做邏輯上的切割

methods方法

attributes屬性

<?php
    //先建立物件(類似建立一個模組)
    class People{
        public $weight = 70;
        public function getWeight(){
            return $this->weight;
        }
        public function eat($food){
            $this->weight += $food;
            return $this->weight;
        }
    }
    
//使用物件
$people = new People;
echo($people->getWeight()."\\r\\n"); //70
echo($people->eat(50)."\\r\\n"); //70+50
echo($people->getWeight()."\\r\\n"); //120  因為會因為執行後的結果紀錄故會產生新的數值
?>

繼承

現在我們有一個people物件,那我們會讓其他人來繼承我們的物件

這裡我就是讓john來繼承這個people的行為