Six pre-defined interfaces in PHP

Source: Internet
Author: User
Tags rewind
This article mainly introduces six pre-defined interfaces in PHP. This article describes Traversable, Iterator, IteratorAggregate, ArrayAccess, Serializable, Closure, you can refer to PHP to describe 6 predefined interfaces as follows:

1. Traversable traversal interface

Haha! In fact, it is not an interface that can be used in PHP, and internal classes can be used. it has a purpose to detect whether a class can be traversed.

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

2. Iterator interface

Interface abstract:

Iterator extends Traversable {// returns the abstract public mixed current (void) element pointed to by the current index cursor // returns the abstract public scalar key (void) key name of the element pointed to by the current index cursor) // Move the current index cursor to the next element abstract public void next (void) // reset the index cursor to the first element abstract public void rewind (void) // Determine whether the current index cursor points to an element. it is often used to call rewind () or next () to use abstract public boolean valid (void )}

The above allows a class to implement a basic iteration function, as shown in the following figure:

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 aggregate iterator interface

Interface abstract:

IteratorAggregate extends Traversable {// Obtain the external iterator abstract public Traversable getIterator (void )}

GetIterator is an instance of the class of an Iterator or a Traversable interface. Obtain the external iterator for iterative access as follows.

class myData implements IteratorAggregate {  public $property1 = "Public property one" ;  public $property2 = "Public property two" ;  public $property3 = "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 access interface

Interface abstract:

ArrayAccess {/* method */abstract public boolean offsetExists (mixed $ offset) // check whether the offset position contains abstract public mixed offsetGet (mixed $ offset) // obtain the value of an offset position abstract public void offsetSet (mixed $ offset, mixed $ value) // set the value of an offset position abstract public void offsetUnset (mixed $ offset) // reset the value at an offset position}

You can access an object like an array as follows:

class obj implements arrayaccess {  private $container = array();  public function __construct () {    $this -> container = array(      "one"  => 1 ,      "two"  => 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 [ "two" ]));var_dump ( $obj [ "two" ]);unset( $obj [ "two" ]);var_dump (isset( $obj [ "two" ]));$obj [ "two" ] = "A value" ;var_dump ( $obj [ "two" ]);$obj [] = 'Append 1' ;$obj [] = 'Append 2' ;$obj [] = 'Append 3' ;print_r ( $obj );

5. Serializable serialization interface

Interface abstract:

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

Classes implementing this interface no longer support _ sleep () and _ wakeup (). It is easy to use. as long as the serialize method is called during object serialization, the unserialize method is called during deserialization.

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 );print_r($ser);$newobj = unserialize ( $ser );print_r($newobj);


6. Closure
Interface abstract:

Closure {/* method */_ construct (void) // The public static Closure bind (Closure $ closure, object $ newthis [, mixed $ newscope = 'static ']) // Copy a closure and bind it to the specified $ this object and class scope. Public Closure bindTo (object $ newthis [, mixed $ newscope = 'static ']) // copy the current Closure object and bind the specified $ this object and class scope .}

class A {  private static $sfoo = 1 ;  private $ifoo = 2 ;} $cl1 = static function() {  return A :: $sfoo ;}; $cl2 = function() {  return $this -> ifoo ;};  $bcl1 = Closure :: bind ( $cl1 , null , 'A' ); $bcl2 = Closure :: bind ( $cl2 , new A (), 'A' );echo $bcl1 (), "\n" ;echo $bcl2 (), "\n" ;

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.