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

首頁 > 開發 > PHP > 正文

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

2024-05-04 23:35:04
字體:
來源:轉載
供稿:網友

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

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

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. function rewind () { 
  13. var_dump ( __METHOD__ ); 
  14. $this -> position = 0 ; 
  15.  
  16. function current () { 
  17. var_dump ( __METHOD__ ); 
  18. return $this -> array [ $this -> position ]; 
  19.  
  20. function key () { 
  21. var_dump ( __METHOD__ ); 
  22. return $this -> position ; 
  23.  
  24. function next () { 
  25. var_dump ( __METHOD__ ); 
  26. ++ $this -> position ; 
  27.  
  28. function valid () { 
  29. var_dump ( __METHOD__ ); 
  30. return isset( $this -> array [ $this -> position ]); 
  31.  
  32. $it = new myIterator ; 
  33.  
  34. foreach$it as $key => $value ) { 
  35. var_dump ( $key , $value ); 
  36. echo "/n" ; 

3.IteratorAggregate聚合式迭代器接口

接口摘要:

 

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

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. public function getIterator () { 
  11. return new ArrayIterator ( $this ); 
  12.  
  13. $obj = new myData ; 
  14.  
  15. foreach$obj as $key => $value ) { 
  16. var_dump ( $key , $value ); 
  17. 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. public function offsetSet ( $offset , $value ) { 
  10. if ( is_null ( $offset )) { 
  11. $this -> container [] = $value ; 
  12. else { 
  13. $this -> container [ $offset ] = $value ; 
  14. public function offsetExists ( $offset ) { 
  15. return isset( $this -> container [ $offset ]); 
  16. public function offsetUnset ( $offset ) { 
  17. unset( $this -> container [ $offset ]); 
  18. public function offsetGet ( $offset ) { 
  19. return isset( $this -> container [ $offset ]) ? $this -> container [ $offset ] : null ; 
  20.  
  21. $obj = new obj ; 
  22.  
  23. var_dump (isset( $obj [ "two" ])); 
  24. var_dump ( $obj [ "two" ]); 
  25. unset( $obj [ "two" ]); 
  26. var_dump (isset( $obj [ "two" ])); 
  27. $obj [ "two" ] = "A value" ; 
  28. var_dump ( $obj [ "two" ]); 
  29. $obj [] = 'Append 1' ; 
  30. $obj [] = 'Append 2' ; 
  31. $obj [] = 'Append 3' ; 
  32. 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. public function serialize () { 
  6. return serialize ( $this -> data ); 
  7. public function unserialize ( $data ) { 
  8. $this -> data = unserialize ( $data ); 
  9. public function getData () { 
  10. return $this -> data ; 
  11.  
  12. $obj = new obj ; 
  13. $ser = serialize ( $obj ); 
  14. print_r($ser); 
  15. $newobj = unserialize ( $ser ); 
  16. 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對象和類作用域。 

 

 
  1. class A { 
  2. private static $sfoo = 1 ; 
  3. private $ifoo = 2 ; 
  4. $cl1 = static function() { 
  5. return A :: $sfoo ; 
  6. }; 
  7. $cl2 = function() { 
  8. return $this -> ifoo ; 
  9. }; 
  10.  
  11. $bcl1 = Closure :: bind ( $cl1 , null , 'A' ); 
  12. $bcl2 = Closure :: bind ( $cl2 , new A (), 'A' ); 
  13. echo $bcl1 (), "/n" ; 
  14. echo $bcl2 (), "/n" ; 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沙洋县| 满洲里市| 冕宁县| 莒南县| 宽甸| 安仁县| 两当县| 双柏县| 安阳市| 壤塘县| 会泽县| 安丘市| 兴山县| 无为县| 滨海县| 溧阳市| 桐庐县| 盐亭县| 北碚区| 垣曲县| 江陵县| 扶风县| 介休市| 高密市| 嘉禾县| 万年县| 邯郸市| 延庆县| 仁怀市| 龙山县| 辽宁省| 逊克县| 神池县| 巫溪县| 抚顺县| 和林格尔县| 攀枝花市| 武川县| 克东县| 农安县| 商南县|