天天看點

PHP接口(interface)和抽象類(abstract)

interface

定義了一個接口類,它裡面的方法其子類必須實作。接口是類的一個模闆,其子類必須實作接口中定義的所有方法。 interface user{     function getheight($height); getweight($weight);  } class my implements user{ getheight($username){         echo $height; getweight($weight){ $weight;

abstract

抽象類就把類像的部分抽出來,就是把重複的東西寫到抽象類中,減少工作量。隻要方法沒有用abstract聲明,在其子類中就不用實作。而且在子類中該方法為公共方法。
abstract user{     abstract function getheight($height); getweight(){ class my extends getheight($height){          echo getinfobyid($id){ $this->getweight.“<br/>”;         echo $id;