Introduction to 6 predefined interfaces in PHP
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
Actually, it is not an interface that can be used in PHP, the inner class can be used, and it has a purpose of detecting whether a class can traverse.
?
1 2 3 |
if ($class instanceof traversable) { Foreach } |
2.Iterator Iterator interface
Interface Summary:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Iterator extends Traversable { Returns the element to which the current index cursor is pointing Abstract public mixed current (void) Returns the key name of the element to which the current index cursor is pointing Abstract public scalar key (void) Moves the current index cursor toward the next element Abstract public void next (void) Resets the pointer to the first element of an index cursor Abstract public void rewind (void) Determines whether the current index cursor points to an element, often in a call to rewind () or next () using the Abstract public boolean 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:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
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__); + position, $this + +; } 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:
?
1 2 3 4 5 |
Iteratoraggregate extends Traversable { Get External iterators 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.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st |
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:
?
1 2 3 4 5 6 7 |
arrayaccess { /* Method */ Abstract public boolean offsetexists (mixed $offset)//check for the existence of an offset position Abstract public mixed offsetget (mixed $offset)//Get the value of an offset position Abstract public void Offsetset (mixed $offset, mixed $value)//Set a value for offset position 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:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
Class Obj implements Arrayaccess { Private $container = Array (); Public Function __construct () { $this-container = Array ( "One" = 1, "Both" = 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 ["the"]); unset ($obj ["the"]); Var_dump (Isset ($obj ["the"]); $obj ["" "] =" A value "; Var_dump ($obj ["the"]); $obj [] = ' Append 1 '; $obj [] = ' Append 2 '; $obj [] = ' Append 3 '; Print_r ($obj); |
5.Serializable Serialization Interface
Interface Summary:
?
1 2 3 4 5 6 |
Serializable { /* Method */ Abstract public string representation of serialize (void)//Object 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.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st |
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:
?
1 2 3 4 5 6 |
Closure { /* Method */ __construct (void)//constructor to disallow instantiation public static Closure bind (Closure $closure, Object $newthis [, Mixed $newscope = ' static '])//Copy a closure, bind the specified $this object and class as Use the domain. Public Closure BindTo (Object $newthis [, Mixed $newscope = ' static '])//Copy the current closure object, binding the specified $this object and class scope. } |
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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"; |
http://www.bkjia.com/PHPjc/1000103.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000103.html techarticle 6 types of interfaces predefined in PHP this article mainly introduces the 6 kinds of interfaces that are predefined in PHP, this article explains Traversable, Iterator, Iteratoraggregate, arrayaccess, Serializa ...