函式

將不同邏輯包裝成一個函式(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屬性

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/79298d0f-e7c9-4a66-a89a-dd1a594ffbbd/1626586412177.jpg

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/204d02ba-d1cd-43a5-a991-c92d90e01db9/1626586593218.jpg

<?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  因為會因為執行後的結果紀錄故會產生新的數值
?>

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/90e3965a-72ab-4c6a-ad34-a2041aa6ef6a/1626587223147.jpg

繼承

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

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/ccca8d49-8a72-4d63-96ce-42a84e452a82/1626587281678.jpg

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

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/43f56fa7-84c4-4f4b-b6ea-09f9af9cb506/1626587437143.jpg