About 6 predefined interfaces in PHP

Source: Internet
Author: User
Tags rewind
This article mainly introduces the 6 predefined interfaces introduced in PHP, this article explains Traversable, Iterator, Iteratoraggregate, arrayaccess, Serializable, Closure, A friend you need can refer to the following

PHP pre-defined 6 interfaces are described below:

1.Traversable Traversal Interface

Oh! In fact, it is not an interface that can be used in PHP, an internal class is used, and it has a purpose of detecting whether a class can traverse.

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

2.Iterator Iterator interface

Interface Summary:

Iterator extends Traversable {   //returns the element that the current index cursor points to   abstract public mixed current (void)   //Returns the key name of the element to which the currently indexed cursor is pointing   Abstract public scalar key (void)   //move current index cursor point to next element   abstract public void next (void)   //Reset index cursor to first element   abstract public void rewind (void)   //Determines whether the current index cursor points to an element, often using the abstract public Boolean in the call to rewind () or next ()   Valid (void)}

The above can allow a class to implement a basic iterative function, as follows you can see the sequence of calls to the iteration:

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 converged Iterator interface

Interface Summary:

Iteratoraggregate extends Traversable {//Get External iterator abstract public traversable getiterator (void)}

Getiterator is an instance of a class that is a iterator or traversable interface. The following gets an external iterator to implement iterative access.

Class MyData implements Iteratoraggregate {public  $property 1 = ' public property one ';  Public $property 2 = "Public property";  Public $property 3 = "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 array-Type access interface

Interface Summary:

arrayaccess {  /* method *  /Abstract public boolean offsetexists (mixed $offset)//check if an  abstract public mix exists in the offset position Ed Offsetget (mixed $offset)//Get the value of an offset position  abstract public void Offsetset (mixed $offset, mixed $value)//set an offset position The value of the  abstract public void Offsetunset (mixed $offset)//Resets the value of an offset position}

The object can be accessed as if it were accessed as an array:

Class Obj implements Arrayaccess {private $container = array (); Public Function __construct () {$this-container = Array ("one" + = 1, "one" + = 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 ["the"]), Var_dump ($obj ["" "]), unset ($obj [" ""]), Var_dump (Isset ($obj ["the"]); $obj ["A"] = "A value"; Var_dump ($obj ["]"); $obj [] = ' Append 1 '; $obj [] = ' Append 2 '; $obj [] = ' Append 3 ';p rint _r($obj); 

5.Serializable Serialization Interface

Interface Summary:

The Serializable {   /* method */Abstract public string  serialize (void)//object represents the  abstract public mixed unserialize (string $serialized)//Construct Object}

Classes that implement this interface no longer support __sleep () and __wakeup (). It is very simple to use, as long as the Serialize method is called when serializing the object, and when deserializing, the Unserialize method is called.

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);p Rint_r ($ser ); $newobj = Unserialize ($ser);p rint_r ($newobj);

6.Closure
Interface Summary:

Closure {  /* method *  /__construct (void)//constructor for disallow instantiation public  static Closure bind (Closure $closure, Object $newthis [, Mixed $newscope = ' static '])//Copy a closure, bind the specified $this object and class scope. Public  Closure BindTo (Object $newthis [, Mixed $newscope = ' static '])//Copy the current closure object, binding the specified $this object and class scope. }
Class A {  private static $sfoo = 1;  Private $ifoo = 2;} $CL 1 = static function () {  return A:: $sfoo;}; $cl 2 = function () {  return $this-IFoo;};  $BCL 1 = Closure:: Bind ($CL 1, NULL, ' A '); $BCL 2 = Closure:: Bind ($CL 2, New A (), ' A '), echo $BCL 1 (), "\ n", Echo $BCL 2 (), "\ n";

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

The decision and dynamic definition of PHP variables

PHP array converted to Apple plist XML or text Format function

PHP implementation of the acquisition of China Proxy Server network

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.