亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP中預(yù)定義的6種接口介紹

 更新時間:2015年05月12日 09:04:48   投稿:junjie  
這篇文章主要介紹了PHP中預(yù)定義的6種接口介紹,本文講解了Traversable、Iterator、IteratorAggregate、ArrayAccess、Serializable、Closure,需要的朋友可以參考下

PHP預(yù)定義了6個接口介紹如下:

1.Traversable遍歷接口

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

if($class instanceof Traversable) {
  //foreach
}

2.Iterator迭代器接口

接口摘要:

Iterator extends Traversable 
{ 
  //返回當(dāng)前索引游標(biāo)指向的元素 
  abstract public mixed current(void) 
  //返回當(dāng)前索引游標(biāo)指向的元素的鍵名 
  abstract public scalar key(void) 
  //移動當(dāng)前索引游標(biāo)指向下一元素 
  abstract public void next(void) 
  //重置索引游標(biāo)的指向第一個元素 
  abstract public void rewind(void) 
  //判斷當(dāng)前索引游標(biāo)指向的是否是一個元素,常常在調(diào)用 rewind()或 next()使用 
  abstract public boolean valid(void) 
} 

以上可以讓一個類實現(xiàn)一個基本的迭代功能,如下可以看到迭代的調(diào)用順序:

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聚合式迭代器接口

接口摘要:

IteratorAggregate extends Traversable {
 
//獲取外部迭代器
abstract public Traversable getIterator ( void )
} 

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

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數(shù)組式訪問接口

接口摘要:

ArrayAccess {
  /* 方法 */
  abstract public boolean offsetExists ( mixed $offset ) //檢查偏移位置是否存在
  abstract public mixed offsetGet ( mixed $offset ) //獲取一個偏移位置的值
  abstract public void offsetSet ( mixed $offset , mixed $value ) //設(shè)置一個偏移位置的值
  abstract public void offsetUnset ( mixed $offset ) //復(fù)位一個偏移位置的值
}

如下可像訪問數(shù)組一樣訪問對象:

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序列化接口

接口摘要:

Serializable {
 
  /* 方法 */
  abstract public string serialize ( void ) //對象的字符串表示
  abstract public mixed unserialize ( string $serialized ) // 構(gòu)造對象
}

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

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
接口摘要:

Closure {
  /* 方法 */
  __construct ( void ) //用于禁止實例化的構(gòu)造函數(shù)
  public static Closure bind ( Closure $closure , object $newthis [, mixed $newscope = 'static' ] ) //復(fù)制一個閉包,綁定指定的$this對象和類作用域。
  public Closure bindTo ( object $newthis [, mixed $newscope = 'static' ] ) //復(fù)制當(dāng)前閉包對象,綁定指定的$this對象和類作用域。
}

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" ;

相關(guān)文章

  • PHP 截取字符串 分別適合GB2312和UTF8編碼情況

    PHP 截取字符串 分別適合GB2312和UTF8編碼情況

    主要包括截取GB2312中文字符串,截取utf8編碼的多字節(jié)字符串,UTF-8、GB2312都支持的漢字截取函數(shù),BugFree 的字符截取函數(shù),推薦使用字符串截取功能的朋友。
    2009-02-02
  • PHP實現(xiàn)表單提交數(shù)據(jù)的驗證處理功能【防SQL注入和XSS攻擊等】

    PHP實現(xiàn)表單提交數(shù)據(jù)的驗證處理功能【防SQL注入和XSS攻擊等】

    這篇文章主要介紹了PHP實現(xiàn)表單提交數(shù)據(jù)的驗證處理功能,可實現(xiàn)防SQL注入和XSS攻擊等,涉及php字符處理、編碼轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下
    2017-07-07
  • PHP sprintf()函數(shù)用例解析

    PHP sprintf()函數(shù)用例解析

    sprintf() 函數(shù)把格式化的字符串寫寫入一個變量中。
    2011-05-05
  • php利用ZipArchive類操作文件的實例

    php利用ZipArchive類操作文件的實例

    在本篇文章里小編給大家整理了關(guān)于php利用ZipArchive類實現(xiàn)文件壓縮與解壓實例代碼內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2020-01-01
  • zen cart新進(jìn)商品的隨機(jī)排序修改方法

    zen cart新進(jìn)商品的隨機(jī)排序修改方法

    由于新進(jìn)商品的數(shù)量可能比頁面上能顯示的數(shù)量多,所以采用的是隨機(jī)顯示的方式。如果想改為按時間順序顯示,可以按照radnows提供的 方法修改。(記住使用文件替代)
    2010-09-09
  • PHP獲取數(shù)組最后一個值的2種方法

    PHP獲取數(shù)組最后一個值的2種方法

    這篇文章主要介紹了PHP獲取數(shù)組最后一個值的2種方法,本文直接給出實現(xiàn)代碼,代碼中包含注釋,需要的朋友可以參考下
    2015-01-01
  • PHP檢查網(wǎng)站是否宕機(jī)的方法示例

    PHP檢查網(wǎng)站是否宕機(jī)的方法示例

    這篇文章主要介紹了PHP檢查網(wǎng)站是否宕機(jī)的方法,結(jié)合具體實例形式分析了php基于curl會話針對網(wǎng)站狀態(tài)進(jìn)行檢查的相關(guān)操作技巧,需要的朋友可以參考下
    2017-07-07
  • php生成rss類用法實例

    php生成rss類用法實例

    這篇文章主要介紹了php生成rss類用法,實例分析了使用rssbuilder.class.php類文件生成rss的技巧,需要的朋友可以參考下
    2015-04-04
  • PHP實現(xiàn)的簡易版圖片相似度比較

    PHP實現(xiàn)的簡易版圖片相似度比較

    這篇文章主要介紹了PHP實現(xiàn)的簡易版圖片相似度比較,本文直接給出實現(xiàn)代碼,使用方法請看代碼中的注釋,需要的朋友可以參考下
    2015-01-01
  • PHP 7.1中利用OpenSSL代替Mcrypt加解密的方法詳解

    PHP 7.1中利用OpenSSL代替Mcrypt加解密的方法詳解

    最近在開發(fā)微信公眾號功能的時候發(fā)現(xiàn)在PHP 7.1中Mcrypt已經(jīng)被棄用了,無奈只能找對應(yīng)的解決方法來替代,所以這篇文章主要給大家介紹了關(guān)于在PHP 7.1中利用OpenSSL代替Mcrypt加解密的相關(guān)資料,需要的朋友可以參考下。
    2017-11-11

最新評論