[TOC]
* * * * *
##1 接口意義
1 接口用來**約定類的功能**。指定類必須實現的功能方法。
2 接口文件中使用**interface**關鍵字聲明接口,在其中**列出需要實現的功能方法名稱**。
3 接口中的方法的**修飾符必須是public**,并且**方法體為空**。
4 接口也可以使用**extends擴展接口**,也可**定義常量**。
##2 接口使用
### 2-1 接口簡單使用示例
~~~
<?php
// 聲明'iTemplate'接口,列出需要實現的功能方法名稱
interface iTemplate
{
public function setVariable ( $name , $var );
public function getHtml ( $template );
}
// 創建類實現接口中的方法。
class Template implements iTemplate
{
private $vars = array();
public function setVariable ( $name , $var )
{
$this -> vars [ $name ] = $var ;
}
public function getHtml ( $template )
{
foreach( $this -> vars as $name => $value ) {
$template = str_replace ( '{' . $name . '}' , $value , $template );
}
return $template ;
}
}
~~~
### 2-2 使用extends擴展接口
~~~
// 使用接口b擴展接口a
interface a
{
public function foo ();
}
interface b extends a
{
public function baz ( Baz $baz );
}
//創建類實現擴展后的接口b
class c implements b
{
public function foo ()
{
}
public function baz ( Baz $baz )
{
}
}
~~~
~~~
//創建兩個并列接口
interface a
{
public function foo ();
}
interface b
{
public function bar ();
}
//接口c同時繼承a與b接口
interface c extends a , b
{
public function baz ();
}
//創建類d實現接口c
class d implements c
{
public function foo ()
{
}
public function bar ()
{
}
public function baz ()
{
}
}
~~~
###2-3 在接口中使用常量
~~~
<?php
//接口a中包含常量b
interface a
{
const b = 'Interface constant' ;
}
//輸出接口常量
echo a :: b ;
~~~
## 3 常用接口
>[info] php中預定義了特定功能接口。
### 3-1 迭代器接口Iterator
> 1 接口功能方法
~~~
Iterator extends Traversable {
//返回當前鍵的值
abstract public mixed current ( void )
//返回當前鍵名
abstract public scalar key ( void )
//移動到下一個
abstract public void next ( void )
//返回開頭
abstract public void rewind ( void )
//檢查當前鍵名是否有效
abstract public boolean valid ( void )
}
~~~
> 2 接口實例
~~~
<?php
;自定義類實現迭代器接口功能方法
class myIterator implements Iterator {
private $position = 0 ;
private $array = array(
"firstelement" ,
"secondelement" ,
"lastelement" ,
);
public function __construct () {
$this -> position = 0 ;
}
function rewind () {
var_dump ( __METHOD__ );
$this -> position = 0 ;
}
function current () {
var_dump ( __METHOD__ );
return $this -> array [ $this -> position ];
}
function key () {
var_dump ( __METHOD__ );
return $this -> position ;
}
function next () {
var_dump ( __METHOD__ );
++ $this -> position ;
}
function valid () {
var_dump ( __METHOD__ );
return isset( $this -> array [ $this -> position ]);
}
}
;創建迭代器對象
$it = new myIterator ;
;使用foreach遍歷迭代器對象
foreach( $it as $key => $value ) {
var_dump ( $key , $value );
echo "\n" ;
}
?>
~~~
輸出
~~~
string(18) "myIterator::rewind"
string(17) "myIterator::valid"
string(19) "myIterator::current"
string(15) "myIterator::key"
int(0)
string(12) "firstelement"string(16) "myIterator::next"
string(17) "myIterator::valid"
string(19) "myIterator::current"
string(15) "myIterator::key"
int(1)
string(13) "secondelement"string(16) "myIterator::next"
string(17) "myIterator::valid"
string(19) "myIterator::current"
string(15) "myIterator::key"
int(2)
string(11) "lastelement"string(16) "myIterator::next"
string(17) "myIterator::valid"
~~~
**PHP內置了SPL迭代器**
### 3-2 數組訪問接口ArrayAccess
> 1 接口功能方法
~~~
ArrayAccess {
;檢查數組下標是否有效
abstract public boolean offsetExists ( mixed $offset )
;獲取數組下標的對應值
abstract public mixed offsetGet ( mixed $offset )
;修改數組下標的對應值
abstract public void offsetSet ( mixed $offset , mixed $value )
;刪除數組下標的值
abstract public void offsetUnset ( mixed $offset )
}
~~~
> 2 接口使用
~~~
;創建類實現數組訪問接口
class obj implements Arrayaccess {
private $container = array();
public function __construct () {
$this -> container = array(
"one" => 1 ,
"two" => 2 ,
"three" => 3 ,
);
}
public function offsetSet ( $offset , $value ) {
if ( is_null ( $offset )) {
$this -> container [] = $value ;
} else {
$this -> container [ $offset ] = $value ;
}
}
public function offsetExists ( $offset ) {
return isset( $this -> container [ $offset ]);
}
public function offsetUnset ( $offset ) {
unset( $this -> container [ $offset ]);
}
public function offsetGet ( $offset ) {
return isset( $this -> container [ $offset ]) ? $this -> container [ $offset ] : null ;
}
}
;創建數組訪問對象
$obj = new obj ;
;數組訪問操作
var_dump (isset( $obj [ "two" ]));
var_dump ( $obj [ "two" ]);
unset( $obj [ "two" ]);
var_dump (isset( $obj [ "two" ]));
$obj [ "two" ] = "A value" ;
var_dump ( $obj [ "two" ]);
$obj [] = 'Append 1' ;
$obj [] = 'Append 2' ;
$obj [] = 'Append 3' ;
print_r ( $obj );
~~~
輸出
~~~
bool(true)
int(2)
bool(false)
string(7) "A value"
obj Object
(
[container:obj:private] => Array
(
[one] => 1
[three] => 3
[two] => A value
[0] => Append 1
[1] => Append 2
[2] => Append 3
))
~~~
### 3-3 Json序列化接口JsonSerializable
> 1 接口功能方法
~~~
JsonSerializable {
;指定要被序列化的數據
abstract public mixed jsonSerialize ( void )
}
~~~
> 2 接口使用實例
實例1
~~~
<?php
;創建類實現序列化接口
class ArrayValue implements JsonSerializable {
public function __construct (array $array ) {
$this -> array = $array ;
}
public function jsonSerialize () {
return $this -> array ;
}
}
;構造參數
$array = [ 1 , 2 , 3 ];
;序列化輸出json結構數據
echo json_encode (new ArrayValue ( $array ), JSON_PRETTY_PRINT );
?>
~~~
輸出
~~~
[
1,
2,
3
]
~~~
實例2
~~~
<?php
;創建類實現序列化接口功能
class ArrayValue implements JsonSerializable {
public function __construct (array $array ) {
$this -> array = $array ;
}
public function jsonSerialize () {
return $this -> array ;
}
}
;構造參數
$array = [ 'foo' => 'bar' , 'quux' => 'baz' ];
;序列化輸出json結構數據
echo json_encode (new ArrayValue ( $array ), JSON_PRETTY_PRINT );
?>
~~~
輸出
~~~
{
"foo": "bar",
"quux": "baz"
}
~~~
- 更新記錄
- 概述
- 文件索引
- 函數索引
- 章節格式
- 框架流程
- 前:章節說明
- 主:(index.php)入口
- 主:(start.php)框架引導
- 主:(App.php)應用啟動
- 主:(App.php)應用調度
- C:(Controller.php)應用控制器
- M:(Model.php)數據模型
- V:(View.php)視圖對象
- 附:(App.php)應用啟動
- 附:(base.php)全局變量
- 附:(common.php)模式配置
- 附:(convention.php)全局配置
- 附:(Loader.php)自動加載器
- 附:(Build.php)自動生成
- 附:(Hook.php)監聽回調
- 附:(Route.php)全局路由
- 附:(Response.php)數據輸出
- 附:(Log.php)日志記錄
- 附:(Exception.php)異常處理
- 框架工具
- 另:(helper.php)輔助函數
- 另:(Cache.php)數據緩存
- 另:(Cookie.php)cookie操作
- 另:(Console.php)控制臺
- 另:(Debug.php)開發調試
- 另:(Error.php)錯誤處理
- 另:(Url.php)Url操作文件
- 另:(Loader.php)加載器實例化
- 另:(Input.php)數據輸入
- 另:(Lang.php)語言包管理
- 另:(ORM.php)ORM基類
- 另:(Process.php)進程管理
- 另:(Session.php)session操作
- 另:(Template.php)模板解析
- 框架驅動
- D:(\config)配置解析
- D:(\controller)控制器擴展
- D:(\model)模型擴展
- D:(\db)數據庫驅動
- D:(\view)模板解析
- D:(\template)模板標簽庫
- D:(\session)session驅動
- D:(\cache)緩存驅動
- D:(\console)控制臺
- D:(\process)進程擴展
- T:(\traits)Trait目錄
- D:(\exception)異常實現
- D:(\log)日志驅動
- 使用范例
- 服務器與框架的安裝
- 控制器操作
- 數據模型操作
- 視圖渲染控制
- MVC開發初探
- 模塊開發
- 入口文件定義全局變量
- 運行模式開發
- 框架配置
- 自動生成應用
- 事件與插件注冊
- 路由規則注冊
- 輸出控制
- 多種應用組織
- 綜合應用
- tp框架整合后臺auto架構快速開發
- 基礎原理
- php默認全局變量
- php的魔術方法
- php命名空間
- php的自動加載
- php的composer
- php的反射
- php的trait機制
- php設計模式
- php的系統時區
- php的異常錯誤
- php的輸出控制
- php的正則表達式
- php的閉包函數
- php的會話控制
- php的接口
- php的PDO
- php的字符串操作
- php的curl
- 框架心得
- 心:整體結構
- 心:配置詳解
- 心:加載器詳解
- 心:輸入輸出詳解
- 心:url路由詳解
- 心:模板詳解
- 心:模型詳解
- 心:日志詳解
- 心:緩存詳解
- 心:控制臺詳解
- 框架更新
- 4.20(驗證類,助手函數)
- 4.27(新模型Model功能)
- 5.4(新數據庫驅動)
- 7.28(自動加載)