This article mainly organized the PHP predefined interface, in the usual project process more commonly used four interfaces: Iteratoraggregate (polymeric aggregate iterator Iterator), countable, arrayaccess, Iterator , the need for friends can refer to the next
There are several predefined interfaces in PHP that are more commonly used for four interfaces (Iteratoraggregate (converged aggregate iterator iterator),countable, Arrayaccess,Iterator) give you a detailed introduction.
Iteratoraggregate (converged aggregate iterator iterator) interface
Iteratoraggregate extends traversable {abstract public traversable getiterator (void)}
This interface implements a function-to create an external iterator, how to understand it, when we use foreach to traverse the object, if we do not inherit the iteratoraggregate interface, All of the public properties in the object are traversed (only public $var this form). If the iteratoraggregateis inherited, the object returned by the Getiterator method implemented in the class is used, and it is important to note that the return must be a traversable object or extended from traversable Object, otherwise it throws an exception
See an example of 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 ' = ' Yanruitao ', ' c ' = ' LUL ' U ',]; Public Function Getiterator () {return new Arrayiterator ($this->_data);}} $obj = new My;foreach ($obj as $key + $value) {echo "$key + = $value \ n";} Results: a = Yan b = YANRUITAOC = LULU
Countable interface
countable {abstract public int count (void)}
This interface is used to count the number of objects, how to understand, when we call count on an object, if the function does not inherit countable will always return 1, if inherit the countable Returns the number returned by the implemented count method, and takes a look at the following example:
Class countme{ protected $_mycount = 3; Public function count () { return $this->_mycount; }} $countable = new CountMe (); Echo Count ($countable) ;//Return 1class CountMe implements countable{ protected $_mycount = 3; Public function count () { return $this->_mycount; }} $countable = new CountMe (); Echo count ($countable); Returns the 3ArrayAccess 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 Countme{
protected $_mycount = 3; Public function count () { return $this->_mycount; }} $countable = new CountMe (); Echo Count ($ countable);//Return 1class CountMe implements countable{ protected $_mycount = 3; Public function count () { return $this->_mycount; }} $countable = new CountMe (); Echo Count ($ countable); Returns 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)}
The purpose of this interface is to allow us to access the object like an array, this is how to say, I guess in fact, PHP in the lexical analysis if encountered in the way the array of objects, go back to the object to find out if there is an implementation arrayaccess, if any, do 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 mode, see an example:
Class myobj{} $obj = new myObj; $obj [' Name '];//fatal error:cannot use object of type MYOBJ as array in class MYOBJ Impleme NTS arrayaccess {public Function offsetset ($offset, $value) {echo "Offsetset: {$offset} = {$value}\n";} Public Fu Nction offsetexists ($offset) {echo "offsetexists: {$offset}\n";} Public Function Offsetunset ($offset) {echo "Offsetu Nset: {$offset}\n "; } Public Function Offsetget ($offset) {echo "Offsetget: {$offset}\n";}} $obj = new MyObj; $obj [1] = ' Yan '; isset ($obj [' name ']); unset ($obj [' name ']); $obj [' Yrt '];//output: offsetset:1 = Yan OffsetExists:nameoffsetUnset:nameoffsetGet:yrtclass MYOBJ implements arrayaccess {private $_data = []; public fu Nction 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 '] = ' 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 report notice.
The above object can only be a basic array operation, not even traversal, combined with the previous iteratoraggregate could be 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 '] = 23;foreach ($obj as $key + = $va Lue) {echo "{$key} = {$value}\n";} Output: YRT = Yan 1 = Yan name = Yan Age = 23
Iterator interface:
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)}
The interface of an external iterator or class can be iterated internally, which is an explanation given by the official documentation, and it is not easy to understand, in fact, I feel that this interface implements the functionality and trratoraggregate(document: Create an external iterator interface, the interface directly returns an iterator) similar to But this is in the definition of the class itself implemented, see an 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);} pub Lic 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) {Ech O "{$key} =". Var_export ($value, true). " \ n ";} Output: YRT = ' Yan ' name = ' Yan ' 0 = false1 = ' Yan '
Above this reference to a bird brother of an article about a question (iterator mode), but the bird Brother's judgment valid a little flaw, when the value of the North is false when it will be truncated
Summarize
Said so much as if still did not realize their usefulness, suggested to see Yii2 source code, the source of a lot of use of these things, after watching, you will slowly feel "oh ~ seems to be very useful ...." ”
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!