6 predefined interfaces in PHP introduce _php tips

Source: Internet
Author: User
Tags closure mixed rewind

PHP predefined 6 interfaces are described below:

1.Traversable Traversal Interface

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

if ($class instanceof traversable) {

2.Iterator Iterator interface

Interface Summary:

Iterator extends traversable 
  //returns the element that the current index cursor points to 
  abstract public mixed present (void) 
  // Returns the key name of the element that the current index cursor is pointing to 
  abstract public scalar key (void) 
  //Move the current index cursor to the next element, 
  abstract public void next (void) 
  //Reset index cursor to the first element abstract public 
  void Rewind (void) 
  //To determine whether the current index cursor is pointing to an element that is often invoked rewind () or next () using the 
  Abstract Public boolean valid (void) 

The above allows a class to implement a basic iterative function, and you can see the invocation order of the iterations as follows:

Class Myiterator implements iterator {
  private $position = 0;
  Private $array = Array (
    "Firstelement", "
    secondelement", "
  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 Summary:

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

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

Class MyData implements Iteratoraggregate {public
  $property 1 = ' public property one ';
  Public $property 2 = "two";
  Public $property 3 = "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 the offset location for
  abstract public mix 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
  abstract public void Offsetunset (mixed $offset)//Reset the value of an offset position

You can access an object as if you were accessing an array:

Class Obj implements Arrayaccess {private $container = array (); Public Function __construct () {$this-> container = Array ("One" => 1, "Two" => 2, "t
  Hree "=> 3,); The Public Function Offsetset ($offset, $value) {if (Is_null ($offset)) {$this-> container [] = $val
    else {$this-> container [$offset] = $value;
  The Public Function offsetexists ($offset) {return isset ($this-> container [$offset]);
  The 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 Summary:

Serializable {
  /* method * *
  The string representation of the abstract public string serialize (void)//object represents the
  abstract public mixed unserialize (string $serialized)//Construction Object

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

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);

Interface Summary:

Closure {
  /* method *
  /__construct (void)//The constructor that is used to prohibit instantiating 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, bind 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";

Related Article

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.