This article mainly introduces the predefined 6 kinds of interfaces in PHP, and explains Traversable, Iterator, Iteratoraggregate, Arrayaccess, Serializable and Closure in detail. We hope to help you.
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, Obje CT $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";
Related recommendations:
The actual function of PHP interface class and abstract class
PHP Security Discussion PHP Interface security PHP security detection php.ini Security
PHP interface and Reference Interface _php Tutorial