小記PHP介面概念

2021-08-16 04:38:32 字數 2173 閱讀 9845

介面是更加抽象的抽象類,抽象類中的方法可以有方法體,然而介面裡的所有方法都沒有方法體

基本語法<?php 

inte***ce imyinte***ce

class test implements imyinte***ce

}$test = new test();

$test->sayhi();<?php

inte***ce imyinte***ce

class test implements imyinte***ce

}$test = new test();

$test->sayhi();

將介面當成物件傳入類方法,呼叫介面方法<?php

# 定義介面

inte***ce iusb

# 定義實現類

class phone implements iusb

public function working()

public function stop() }

class tv implements iusb

public function working()

public function stop() }

# 將介面作為物件傳入方法中,可以呼叫介面裡的方法

class computer

}# 例項化

$phone = new phone();

$tv = new tv();

$computer = new computer();

$computer->work($phone);

$computer->work($tv);<?php

# 定義介面

inte***ce iusb

# 定義實現類

class phone implements iusb

public function working()

public function stop() }

class tv implements iusb

public function working()

public function stop() }

# 將介面作為物件傳入方法中,可以呼叫介面裡的方法

class computer

}# 例項化

$phone = new phone();

$tv = new tv();

$computer = new computer();

$computer->work($phone);

$computer->work($tv);

繼承類與介面<?php 

class monkey

public function climbing() }

inte***ce ibird

inte***ce ifish

class littlemonkey extends monkey implements ibird,ifish

public function swim() }

$lm = new littlemonkey('小猴子');

$lm->climbing();

$lm->fly();

$lm->swim();

?><?php

class monkey

public function climbing() }

inte***ce ibird

inte***ce ifish

class littlemonkey extends monkey implements ibird,ifish

public function swim() }

$lm = new littlemonkey('小猴子');

$lm->climbing();

$lm->fly();

$lm->swim();

?>

總結:1.實現介面可以看作是對php單繼承機制的補充;2.實現介面可以在不打破繼承的層級關係的前提下,對類的功能進行擴充套件

介面開發小記

首先,使用者接收資料的時候走的是json格式資料,也就使用php input來接收post流資料。隨後將接收的到的json資料轉換成array格式,進行資料校驗後進行具體的業務 執行。最終返回乙個json或xml格式資料,供使用者讀取接收。先讓我們來看看他們傳送的http請求體資料 傳送格式 con...

介面繼承小記

1.乙個類只能繼承乙個類 乙個類只能繼承乙個類 父類 public class father 子類繼承父類,關鍵字是extends 子類 public class child extends father 2.乙個類可以實現多個介面 乙個類可以實現多個介面 介面一 public inte ce in...

PHP基礎小記

php 超文字預處理器hypertext preprocessor php檔案 可包含文字 html 標籤以及指令碼,在伺服器端執行,向客戶端瀏覽器返回純粹的 html 語法 php指令碼塊以 結束,基礎指令 輸出文字echo print,注釋 同c 變數 以 符號開始,無需宣告變數型別 字串 並置...