There are several predefined interfaces in PHP, more commonly used four interfaces (iteratoraggregate (aggregated aggregate iterator iterator),countable, Arrayaccess,iterator) to give you a detailed introduction.
Iteratoraggregate (aggregated aggregate iterator iterator) interface
Copy Code code as follows:
Iteratoraggregate extends Traversable {
Abstract public traversable getiterator (void)
}
This interface implements a function--create an external iterator, specifically how to understand, when we use foreach to iterate over the object, if we do not inherit the iteratoraggregate interface, All the public properties in the object are traversed (only public $var this form). If you inherit iteratoraggregate, you'll use the object returned by the Getiterator method implemented in the class, and here you should be aware that the return must be a traversable Object or an object that extends from a traversable , otherwise an exception is thrown
See Example
class my{
private $_data = [
' A ' => ' Yan ', '
B ' => ' Yanruitao ',
' C ' => ' LULU ',
]; Public
function Getiterator ()
{return
new Arrayiterator ($this->_data);
}
$obj = new My;
foreach ($obj as $key => $value) {
echo "$key => $value \ n";
}
The output is empty
class my implements Iteratoraggregate {
private $_data = [
' A ' => ' Yan ',
' B ' => ' Yanru Itao ',
' C ' => ' LULU ',
];
Public Function Getiterator ()
{return
new Arrayiterator ($this->_data);
}
$obj = new My;
foreach ($obj as $key => $value) {
echo "$key => $value \ n";
}
Result:
a => Yan
b => Yanruitao
c => LULU
Countable interface
Copy Code code as follows:
countable {
Abstract public int count (void)
}
This interface is used to count the number of objects, and how do you understand that when we call Count on an object, if the function does not inherit countable will always return 1 if you inherit the countable Returns the number returned by the implemented count method, looking at the following example:
class CountMe {protected $_mycount = 3;
Public function count () {return $this->_mycount;
}} $countable = new CountMe ();
echo count ($countable);
Returns 1 class CountMe implements countable {protected $_mycount = 3;
Public function count () {return $this->_mycount;
}} $countable = new CountMe ();
echo count ($countable); Returns the 3 Arrayaccess interface 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 Count
Me {protected $_mycount = 3;
Public function count () {return $this->_mycount;
}} $countable = new CountMe ();
echo count ($countable);
Returns 1 class CountMe implements countable {protected $_mycount = 3;
Public function count () {return $this->_mycount;
}} $countable = new CountMe ();
echo count ($countable); Returns 3
Arrayaccess interface
Copy Code code as follows:
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)
}
The function of this interface is to allow us to access the object like an array, how does this say, I guess is actually PHP in the lexical analysis of the time if you encounter an array of ways to use objects, go back to the object to find whether there is a implementation of arrayaccess if any, the corresponding operation (set, unset, Isset, get) so that we can put an array inside the class, let the class implement the basic operation of the array method, see example below:
Class MyObj {} $obj = new MyObj;
$obj [' name ']; Fatal Error:cannot Use object of type MyObj as array into class MyObj implements Arrayaccess {public Function offset
Set ($offset, $value) {echo "Offsetset: {$offset} => {$value}\n";
The Public Function offsetexists ($offset) {echo "offsetexists: {$offset}\n";
The Public Function Offsetunset ($offset) {echo "Offsetunset: {$offset}\n";
The Public Function Offsetget ($offset) {echo "Offsetget: {$offset}\n";
}} $obj = new MyObj;
$obj [1] = ' Yan ';
Isset ($obj [' name ']);
unset ($obj [' name ']);
$obj [' YRT '];
Output results: offsetset:1 => Yan offsetexists:name offsetunset:name Offsetget:yrt class MyObj implements
{Private $_data = [];
Public Function Offsetset ($offset, $value) {$this->_data[$offset] = $value;
The Public Function offsetexists ($offset) {return isset ($this->_data[$offset]);
The Public Function Offsetunset ($offset) {unset ($this->_data[$offset]); } public FunctIon Offsetget ($offset) {return $this->_data[$offset];
}} $obj = new MyObj;
$obj [' yrt '] = ' Yan ';
Var_dump ($obj [' YRT ']);
Var_dump (Isset ($obj [' YRT ']));
unset ($obj [' YRT ']);
Var_dump (Isset ($obj [' YRT ']));
Var_dump ($obj [' YRT ']);
Output: String (9) "Yan" bool (TRUE) bool (false) notice:undefined INDEX:YRT//The last one will quote Notice
The above object can only be a basic array operation, not even traversal, combined with the previous iteratoraggregate can be a foreach:
Class MyObj implements Arrayaccess, Iteratoraggregate
{
private $_data = [];
Public Function Getiterator ()
{return
new Arrayiterator ($this->_data);
}
......
}
$obj = new MyObj;
$obj [' yrt '] = ' Yan ';
$obj [1] = ' Yan ';
$obj [' name '] = ' Yan ';
$obj [' age '] = N;
foreach ($obj as $key => $value) {
echo "{$key} => {$value}\n";
}
Output:
yrt => Yan
1 => Yan
name => Yan age
=> 23
Iterator interface:
Copy Code code as follows:
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)
}
An interface that can iterate over its own external iterator or class internally, it's an explanation from the official document, and it's hard to understand, but I feel like this interface implements functionality and Trratoraggregate(document: Creating an external iterator interface that interfaces directly back to an iterator) is similar, But this is implemented in the definition of the class itself, see Example:
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 ' => ' Yan ', '
name ' => ' Yan ',
false,
' Yan '
];
$obj = new MyObj ($t);
foreach ($obj as $key => $value) {
echo ' {$key} => '. Var_export ($value, true). " \ n ";
}
Output:
yrt => ' Yan '
name => ' Yan '
0 => false
1 => ' Yan '
This reference to the bird brother's article about a test (iterator mode), but the bird Brother's judgment valid a bit flawed, when the value of the North is false when the time will be truncated
Summarize
Said so much as if still did not realize their usefulness, suggest to see Yii2 source code, the source inside a lot of use these things, after watching, you will slowly feel "oh ~ seems to be very useful ...." ”
All the above is the introduction of this article, I hope you like.