国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP預定義接口使用學習筆記

2024-09-04 11:43:47
字體:
來源:轉載
供稿:網友

我們知道php提供了6個迭代器接口了,那么這6個接口怎么樣呢?有沒有朋友都了解?如果各位朋友不知道的可以和小編一起來看看.

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

pse: collapse; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;">

Traversable 遍歷接口(檢測一個類是否可以使用0 2foreach 0?2進行遍歷的接口)

Iterator 迭代器接口(可在內部迭代自己的外部迭代器或類的接口)

IteratorAggregate 聚合式迭代器接口(創建外部迭代器的接口)

OuterIterator 迭代器嵌套接口(將一個或多個迭代器包裹在另一個迭代器中)

RecursiveIterator 遞歸迭代訪問接口(提供遞歸訪問功能)

SeekableIterator 可索引迭代訪問接口(實現查找功能)

1.Traversable遍歷接口

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

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

2.Iterator迭代器接口,接口摘要:

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

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

  1. class  myIterator  implements  Iterator  { 
  2.     private  $position  =  0 ; 
  3.     private  $array  = array
  4.         "firstelement" , 
  5.         "secondelement" , 
  6.         "lastelement" , 
  7.     ); 
  8.  
  9.     public function  __construct () { 
  10.         $this -> position  =  0 ; 
  11.     } 
  12.  
  13.     function  rewind () { 
  14.         var_dump ( __METHOD__ ); 
  15.         $this -> position  =  0 ; 
  16.     } 
  17.  
  18.     function  current () { 
  19.         var_dump ( __METHOD__ ); 
  20.         return  $this -> array [ $this -> position ]; 
  21.     } 
  22.  
  23.     function  key () { 
  24.         var_dump ( __METHOD__ ); 
  25.         return  $this -> position ; 
  26.     } 
  27.  
  28.     function  next () { 
  29.         var_dump ( __METHOD__ ); 
  30.         ++ $this -> position ; 
  31.     } 
  32.  
  33.     function  valid () { 
  34.         var_dump ( __METHOD__ ); 
  35.         return isset( $this -> array [ $this -> position ]); 
  36.     }  //開源軟件:Vevb.com 
  37.  
  38. $it  = new  myIterator ; 
  39.  
  40. foreach$it  as  $key  =>  $value ) { 
  41.     var_dump ( $key ,  $value ); 
  42.     echo  "\n" ; 

3.IteratorAggregate聚合式迭代器接口,接口摘要:

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

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

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

4.ArrayAccess數組式訪問接口,接口摘要:

  1. ArrayAccess  { 
  2.     /* 方法 */ 
  3.     abstract public boolean offsetExists  ( mixed  $offset  ) //檢查偏移位置是否存在 
  4.     abstract public mixed offsetGet  ( mixed  $offset  ) //獲取一個偏移位置的值 
  5.     abstract public void offsetSet  ( mixed  $offset  , mixed  $value  ) //設置一個偏移位置的值 
  6.     abstract public void offsetUnset  ( mixed  $offset  ) //復位一個偏移位置的值 

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

  1. class  obj  implements  arrayaccess  { 
  2.     private  $container  = array(); 
  3.     public function  __construct () { 
  4.         $this -> container  = array
  5.             "one"    =>  1 , 
  6.             "two"    =>  2 , 
  7.             "three"  =>  3 , 
  8.         ); 
  9.     } 
  10.     public function  offsetSet ( $offset ,  $value ) { 
  11.         if ( is_null ( $offset )) { 
  12.             $this -> container [] =  $value ; 
  13.         } else { 
  14.             $this -> container [ $offset ] =  $value ; 
  15.         } 
  16.     } 
  17.     public function  offsetExists ( $offset ) { 
  18.         return isset( $this -> container [ $offset ]); 
  19.     } 
  20.     public function  offsetUnset ( $offset ) { 
  21.         unset( $this -> container [ $offset ]); 
  22.     } 
  23.     public function  offsetGet ( $offset ) { 
  24.         return isset( $this -> container [ $offset ]) ?  $this -> container [ $offset ] :  null ; 
  25.     } 
  26.  
  27. $obj  = new  obj ; 
  28.  
  29. var_dump (isset( $obj [ "two" ])); 
  30. var_dump ( $obj [ "two" ]); 
  31. unset( $obj [ "two" ]); 
  32. var_dump (isset( $obj [ "two" ])); 
  33. $obj [ "two" ] =  "A value" ; 
  34. var_dump ( $obj [ "two" ]); 
  35. $obj [] =  'Append 1' ; 
  36. $obj [] =  'Append 2' ; 
  37. $obj [] =  'Append 3' ; 
  38. print_r ( $obj ); 

5.Serializable序列化接口,接口摘要:

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

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

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

6.Closure,接口摘要:

  1. Closure  { 
  2.     /* 方法 */ 
  3.     __construct  ( void ) //用于禁止實例化的構造函數 
  4.     public static Closure bind  ( Closure  $closure  , object $newthis  [, mixed  $newscope  = 'static'  ] ) //復制一個閉包,綁定指定的$this對象和類作用域。 
  5.     public Closure bindTo  ( object $newthis  [, mixed  $newscope  = 'static'  ] ) //復制當前閉包對象,綁定指定的$this對象和類作用域。 
  6. class  A  { 
  7.     private static  $sfoo  =  1 ; 
  8.     private  $ifoo  =  2 ; 
  9.  $cl1  = static function() { 
  10.     return  A :: $sfoo ; 
  11. }; 
  12.  $cl2  = function() { 
  13.     return  $this -> ifoo ; 
  14. }; 
  15.  
  16.  $bcl1  =  Closure :: bind ( $cl1 ,  null ,  'A' ); 
  17.  $bcl2  =  Closure :: bind ( $cl2 , new  A (),  'A' ); 
  18. echo  $bcl1 (),  "\n" ; 
  19. echo  $bcl2 (),  "\n" ;

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 神农架林区| 丹巴县| 安义县| 郸城县| 长子县| 景泰县| 恭城| 商城县| 资溪县| 保山市| 侯马市| 舞阳县| 宣恩县| 同江市| 上饶县| 黔南| 临湘市| 高邮市| 大理市| 五大连池市| 会同县| 多伦县| 荣昌县| 商丘市| 美姑县| 江华| 章丘市| 苏州市| 招远市| 阆中市| 凭祥市| 城口县| 宁波市| 凤冈县| 横山县| 西乡县| 扎赉特旗| 白山市| 枣强县| 吉首市| 柳林县|