溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PHP中有哪些預定義接口

發布時間:2021-07-29 16:25:37 來源:億速云 閱讀:161 作者:Leah 欄目:開發技術

本篇文章為大家展示了PHP中有哪些預定義接口,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

IteratorAggregate(聚合式aggregate迭代器Iterator)接口

代碼如下:


IteratorAggregate extends Traversable {
 abstract public Traversable getIterator(void)
}

這個接口實現了一個功能——創建外部迭代器,具體怎么理解呢,當我們使用foreach對對象進行便遍歷的時候,如果沒有繼承IteratorAggregate接口,遍歷的是對象中所有的public屬性(只能是public $var這種形式)。要是繼承了IteratorAggregate,會使用類中實現的getIterator方法返回的對象,這里要注意返回的一定要是一個Traversable對象或者擴展自Traversable的對象,否則會拋出異常

//看個例子
class My{
 private $_data = [
 'a' => '燕睿濤',
 'b' => 'yanruitao',
 'c' => 'LULU',
 ];
 
 public function getIterator()
 {
 return new ArrayIterator($this->_data);
 }
}
$obj = new My;
foreach ($obj as $key => $value) {
 echo "$key => $value\n";
}
//輸出結果為空 

class My implements IteratorAggregate {
 private $_data = [
 'a' => '燕睿濤',
 'b' => 'yanruitao',
 'c' => 'LULU',
 ];

 public function getIterator()
 {
 return new ArrayIterator($this->_data);
 }
}
$obj = new My;
foreach ($obj as $key => $value) {
 echo "$key => $value\n";
}
//結果:
a => 燕睿濤
b => yanruitao
c => LULU

Countable接口

代碼如下:


Countable {
 abstract public int count(void)
}

這個接口用于統計對象的數量,具體怎么理解呢,當我們對一個對象調用count的時候,如果函數沒有繼承Countable將一直返回1,如果繼承了Countable會返回所實現的count方法所返回的數字,看看下面的例子:

class CountMe
{ 
 protected $_myCount = 3; 

 public function count() 
 { 
 return $this->_myCount; 
 } 
} 

$countable = new CountMe(); 
echo count($countable);
//返回1

class CountMe implements Countable
{ 
 protected $_myCount = 3; 

 public function count() 
 { 
 return $this->_myCount; 
 } 
} 

$countable = new CountMe(); 
echo count($countable); 
//返回3


ArrayAccess接口
ArrayAccess {
 abstract public boolean offsetExists(mixed $offset)
 abstract public mixed offsetGet(mixed $offset)
 public void offsetSet(mixed $offset, mixed $value)
 public void offsetUnset(mixed $offset)
}


class CountMe
{ 
 protected $_myCount = 3; 

 public function count() 
 { 
  return $this->_myCount; 
 } 
} 

$countable = new CountMe(); 
echo count($countable);
//返回1

class CountMe implements Countable
{ 
 protected $_myCount = 3; 

 public function count() 
 { 
  return $this->_myCount; 
 } 
} 

$countable = new CountMe(); 
echo count($countable); 
//返回3

ArrayAccess接口

復制代碼 代碼如下:


ArrayAccess {
 abstract public boolean offsetExists(mixed $offset)
    abstract public mixed offsetGet(mixed $offset)
    public void offsetSet(mixed $offset, mixed $value)
    public void offsetUnset(mixed $offset)
}

這個接口的作用是讓我們可以像訪問數組一樣訪問對象,這個怎么說好呢,我猜其實就是php在詞法分析的時候如果碰到了數組的方式使用對象,就回去對象中查找是否有實現ArrayAccess如果有的話,進行對應的操作(set、unset、isset、get),這樣我們就可以在類里面放置一個array,讓類實現數組方式的基本操作,下面看個例子:

class myObj
{
 
}
$obj = new myObj;
$obj['name'];
//Fatal error: Cannot use object of type myObj as array in 

class myObj implements ArrayAccess 
{
 public function offsetSet($offset, $value)
 {
  echo "offsetSet : {$offset} => {$value}\n";
 }

 public function offsetExists($offset)
 {
  echo "offsetExists : {$offset}\n";
 }

 public function offsetUnset($offset)
 {
  echo "offsetUnset : {$offset}\n";
 }

 public function offsetGet($offset)
 {
  echo "offsetGet : {$offset}\n";
 }
}
$obj = new myObj;
$obj[1] = '燕睿濤';
isset($obj['name']);
unset($obj['name']);
$obj['yrt'];

//輸出結果:
offsetSet : 1 => 燕睿濤
offsetExists : name
offsetUnset : name
offsetGet : yrt

class myObj implements ArrayAccess 
{
 private $_data = [];
 public function offsetSet($offset, $value)
 {
  $this->_data[$offset] = $value;
 }

 public function offsetExists($offset)
 {
  return isset($this->_data[$offset]);
 }

 public function offsetUnset($offset)
 {
  unset($this->_data[$offset]);
 }

 public function offsetGet($offset)
 {
  return $this->_data[$offset];
 }
}

$obj = new myObj;
$obj['yrt'] = '燕睿濤';
var_dump($obj['yrt']);
var_dump(isset($obj['yrt']));
unset($obj['yrt']);
var_dump(isset($obj['yrt']));
var_dump($obj['yrt']);

//輸出:
string(9) "燕睿濤"
bool(true)
bool(false)
Notice: Undefined index: yrt //最后一個會報出Notice

上面的對象只能是基本的數組操作,連遍歷都不行,結合之前的IteratorAggregate可以進行foreach:

class myObj implements ArrayAccess, IteratorAggregate
{
private $_data = [];

 public function getIterator()
 {
  return new ArrayIterator($this->_data);
 }

 ......
}
$obj = new myObj;
$obj['yrt'] = '燕睿濤';
$obj[1] = '燕睿濤';
$obj['name'] = '燕睿濤';
$obj['age'] = 23;

foreach ($obj as $key => $value) {
 echo "{$key} => {$value}\n";
}
//輸出:
yrt => 燕睿濤
1 => 燕睿濤
name => 燕睿濤
age => 23

Iterator接口:

復制代碼 代碼如下:


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)
}

可在內部迭代自己的外部迭代器或類的接口,這是官方文檔給出的解釋,看著還是不好理解,其實我感覺這個接口實現的功能和trratorAggregate(文檔:創建外部迭代器接口,接口直接返回一個迭代器)類似,不過這個在類的定義里面自己實現了,看個例子:

class myObj implements Iterator{

 private $_data = [];

 public function __construct(Array $arr)
 {
 $this->_data = $arr;
 }

 public function current()
 {
 return current($this->_data);
 }

 public function key()
 {
 return key($this->_data);
 }

 public function next()
 {
 next($this->_data);
 }

 public function rewind()
 {
 reset($this->_data);
 }

 public function valid()
 {
 return $this->key() !== NULL;
 }
}

$t = [
 'yrt' => '燕睿濤',
 'name' => '燕睿濤',
 false,
 '燕睿濤'
];
$obj = new myObj($t);

foreach ($obj as $key => $value) {
 echo "{$key} => ".var_export($value, true)."\n";
}
//輸出:
yrt => '燕睿濤'
name => '燕睿濤'
0 => false
1 => '燕睿濤'


上述內容就是PHP中有哪些預定義接口,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

php
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女