PHP predefined Interfaces

Source: Internet
Author: User
Tags sorts
This article mainly sorts out the predefined PHP interfaces, which are usually used in the project process: IteratorAggregate (aggregate Iterator), Countable, ArrayAccess, Ite

This article mainly sorts out the predefined PHP interfaces, which are usually used in the project process: IteratorAggregate (aggregate Iterator), Countable, ArrayAccess, Ite

PHP has several predefined interfaces. The four commonly used interfaces (IteratorAggregate (Iterator of aggregate Iterator), Countable, ArrayAccess, and Iterator) are described in detail.

IteratorAggregate (aggregate Iterator) Interface


The Code is as follows:


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

This interface implements a function-create an external iterator. How can we understand it? If we use foreach to traverse objects, if we do not inherit the IteratorAggregate interface, traverse all the public attributes of an object (only in the form of public $ var ). If IteratorAggregate is inherited, the object returned by the getIterator method implemented in the class is used. Note that the returned object must be a Traversable object or an object extended from the Traversable object. Otherwise, an exception is thrown.

// See the example class My {private $ _ data = ['A' => 'yan ruitao', '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 result is empty class My implements IteratorAggregate {private $ _ data = ['A' => 'yan ruitao', 'B' => 'yanruitao ', '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 rutao B => yanruitaoc => LULU

Countable Interface

The Code is as follows:


Countable {
Abstract public int count (void)
}

This interface is used to count the number of objects. How can we understand it? When we call count for an object, if the function does not inherit Countable, 1 is always returned, if Countable is inherited, the number returned by the implemented count method is returned. Let's take a look 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); // return 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 1 class CountMe implements Countable {protected $ _ myCount = 3; public function count () {return $ this-> _ myCount ;}}$ countable = new CountMe (); echo count ($ countable); // returns 3

ArrayAccess Interface

The Code is 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 objects just like accessing arrays. So what should we do? I guess it's true that if php encounters the array method to use objects during lexical analysis, go back to the object to check whether ArrayAccess is implemented. If yes, perform the corresponding operations (set, unset, isset, and get). In this way, we can place an array in the class, let the class implement the basic operation of the array method. The following is an example:

Class myObj {} $ obj = new myObj; $ obj ['name']; // Fatal error: cannot use object of type myObj as array inclass 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] = 'yan ruitao '; isset ($ obj ['name']); unset ($ obj ['name']); $ obj ['yrt ']; // output result: offsetSet: 1 => Yan ruitao offsetExists: nameoffsetUnset: nameoffsetGet: yrtclass 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 '] = 'yan ruitao'; 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 ruitao" bool (true) bool (false) Notice: Undefined index: yrt // the last one will report the Notice

The above object can only be a basic array operation and cannot even be traversed. Combined with the previous IteratorAggregate, you can perform foreach:

Class myObj implements ArrayAccess, IteratorAggregate {private $ _ data = []; public function getIterator () {return new ArrayIterator ($ this-> _ data );}......} $ obj = new myObj; $ obj ['yrt '] = 'yan ritao'; $ obj [1] = 'yan ritao '; $ obj ['name'] = 'yan ruitao '; $ obj ['age'] = 23; foreach ($ obj as $ key => $ value) {echo "{$ key }=>{$ value} \ n" ;}// output: yrt => Yan ritao 1 => Yan ritao name => Yan ritao age => 23

Iterator interface:


The Code is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.