Six pre-defined interfaces in PHP
This article mainly introduces six pre-defined interfaces in PHP. This article describes Traversable, Iterator, IteratorAggregate, ArrayAccess, Serializable, and Closure. For more information, see
PHP predefines six 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.
?
1 2 3 |
If ($ class instanceof Traversable ){ // Foreach } |
2. Iterator Interface
Interface Abstract:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Iterator extends Traversable { // Returns the element pointed to by the current index cursor Abstract public mixed current (void) // Return the key name of the element pointed to by the current index cursor Abstract public scalar key (void) // Move the current index cursor to the next element Abstract public void next (void) // Reset the index cursor pointing to the first element Abstract public void rewind (void) // Determines whether the current index cursor points to an element. It is often used when rewind () or next () is called. Abstract public boolean valid (void) } |
The above allows a class to implement a basic iteration function, as shown in the following figure:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 __); ++ $ 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:
?
1 2 3 4 5 |
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.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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:
?
1 2 3 4 5 6 7 |
ArrayAccess { /* Method */ Abstract public boolean offsetExists (mixed $ offset) // check whether the offset position exists Abstract public mixed offsetGet (mixed $ offset) // gets the value of an offset position. Abstract public void offsetSet (mixed $ offset, mixed $ value) // you can specify a value for an offset. Abstract public void offsetUnset (mixed $ offset) // reset the value of an offset position } |
You can access an object like an array as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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, "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:
?
1 2 3 4 5 6 |
Serializable { /* Method */ Abstract public string serialize (void) // string representation of an object Abstract public mixed unserialize (string $ serialized) // construct an 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.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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:
?
1 2 3 4 5 6 |
Closure { /* Method */ _ Construct (void) // constructor used to disable instantiation 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 it to the specified $ this object and Class scope. } |
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Class { 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 "; |