程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP中預定義的6種接口介紹

PHP中預定義的6種接口介紹

編輯:關於PHP編程

       這篇文章主要介紹了PHP中預定義的6種接口介紹,本文講解了Traversable、Iterator、IteratorAggregate、ArrayAccess、Serializable、Closure,需要的朋友可以參考下

      PHP預定義了6個接口介紹如下:

      1.Traversable遍歷接口

      呵呵!其實它不是一個在PHP中可以使用的接口,內部類才可使用,它有一個用途就是檢測一個類是否可以遍歷。

      ?

    1 2 3 if($class instanceof Traversable) { //foreach }

      2.Iterator迭代器接口

      接口摘要:

      ?

    1 2 3 4 5 6 7 8 9 10 11 12 13 Iterator extends Traversable { //返回當前索引游標指向的元素 abstract public mixed current(void) //返回當前索引游標指向的元素的鍵名 abstract public scalar key(void) //移動當前索引游標指向下一元素 abstract public void next(void) //重置索引游標的指向第一個元素 abstract public void rewind(void) //判斷當前索引游標指向的是否是一個元素,常常在調用 rewind()或 next()使用 abstract public boolean valid(void) }

      以上可以讓一個類實現一個基本的迭代功能,如下可以看到迭代的調用順序:

      ?

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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( $it as $key => $value ) { var_dump ( $key , $value ); echo "n" ; }

      3.IteratorAggregate聚合式迭代器接口

      接口摘要:

      ?

    1 2 3 4 5 IteratorAggregate extends Traversable {   //獲取外部迭代器 abstract public Traversable getIterator ( void ) }

      getIterator是一個Iterator或Traversable接口的類的一個實例。如下獲取外部迭代器實現迭代訪問。

      ?

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class myData implements IteratorAggregate { public $property1 = "Public property one" ; public $property2 = "Public property two" ; public $property3 = "Public property three" ;   public function __construct () { $this -> property4 = "last property" ; }     public function getIterator () { return new ArrayIterator ( $this ); } }   $obj = new myData ;   foreach( $obj as $key => $value ) { var_dump ( $key , $value ); echo "n" ; }

      4.ArrayAccess數組式訪問接口

      接口摘要:

      ?

    1 2 3 4 5 6 7 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 ) //復位一個偏移位置的值 }

      如下可像訪問數組一樣訪問對象:

      ?

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 );

      5.Serializable序列化接口

      接口摘要:

      ?

    1 2 3 4 5 6 Serializable {   /* 方法 */ abstract public string serialize ( void ) //對象的字符串表示 abstract public mixed unserialize ( string $serialized ) // 構造對象 }

      實現該接口的類不再支持__sleep()和__wakeup()。使用很簡單,只要序列化對象時serialize方法會被調用,當反序列化時,unserialize方法被調用。

      ?

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class obj implements Serializable { private $data ; public function __construct () { $this -> data = "My private data" ; } public function serialize () { return serialize ( $this -> data ); } public function unserialize ( $data ) { $this -> data = unserialize ( $data ); } public function getData () { return $this -> data ; } }   $obj = new obj ; $ser = serialize ( $obj ); print_r($ser); $newobj = unserialize ( $ser ); print_r($newobj);

      6.Closure

      接口摘要:

      ?

    1 2 3 4 5 6 Closure { /* 方法 */ __construct ( void ) //用於禁止實例化的構造函數 public static Closure bind ( Closure $closure , object $newthis [, mixed $newscope = 'static' ] ) //復制一個閉包,綁定指定的$this對象和類作用域。 public Closure bindTo ( object $newthis [, mixed $newscope = 'static' ] ) //復制當前閉包對象,綁定指定的$this對象和類作用域。 }

      ?

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class A { private static $sfoo = 1 ; private $ifoo = 2 ; } $cl1 = static function() { return A :: $sfoo ; }; $cl2 = function() { return $this -> ifoo ; };   $bcl1 = Closure :: bind ( $cl1 , null , 'A' ); $bcl2 = Closure :: bind ( $cl2 , new A (), 'A' ); echo $bcl1 (), "n" ; echo $bcl2 (), "n" ;
    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved