PHP Predefined interfaces Use learning Notes

Source: Internet
Author: User
Tags abstract foreach bind closure mixed rewind

PHP predefined 6 interfaces are described below:

Traversable Traversal interface (detects whether a class can use 0 2foreach 0 2 to traverse the interface)
Iterator Iterator interface (an interface that can iterate over its own external iterator or class internally)
Iteratoraggregate Aggregation iterator interface (interface to create external iterator)
Outeriterator Iterator nesting interface (wrapping one or more iterators in another iterator)
Recursiveiterator Recursive iterative Access interface (provides recursive access function)
Seekableiterator Indexed iterative access interface (to implement lookup function)

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) {
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 that the current index cursor points to
Abstract public scalar key (void)
Moves the current index cursor to the next element
Abstract public void next (void)
Resets the index cursor to point to the first element
Abstract public void rewind (void)
Determines whether the current index cursor is pointing to an element and 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",
"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 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 exists
Abstract public mixed offsetget (mixed $offset)//Get 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 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,
"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 Summary:
Serializable {

/* Method * *
Abstract public string serialize (void)//object string representation
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);

6.Closure


Interface Summary:
Closure {
/* Method * *
__construct (void)//constructor that is used to prevent instantiation
public static Closure bind (Closure $closure, Object $newthis [, Mixed $newscope = ' static '])//Copy a closure, bind the specified $th is 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;
    Priva te  $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.