PHP 5 comes with object-oriented programming and provides comprehensive and robust support. you can structure your code in very easily using this OOPs features provided by PHP5.
The main advantage of Interfaces in PHP is that you can implement multiple inheritance which is not supported by the class.In this article, I will show you Predefined interface in PHP.How Predefined Interfaces helps to manage code, What is the importance of Interfaces and type of Predefined interface.
PHP provides a number of predefined Interfaces which are extremely powerful and to help improve both quality and quantity of code.If you don’t have an idea about Interface in PHP, then I would suggest reading the previous article about Interface In PHP.
List of Prefined Interfaces
1. Traversable – Traversal Interfaces is used to detect whether a class can be traversable using foreach loop.
2. Iterator – Iterator Interfaces is to create an external iterator or for internal iteration.
3. IteratorAggregate – IteratorAggregate Interface is used to create an external iterator interface.
4. ArrayAccess – ArrayAccess is an Interface is used for accessing objects as arrays.
5. Serializable – Serializable in an Interface to serialize the data.
6. Closure – Closure is useful to accept a callback function
1. Traversable Interface
In fact, Traversable interface is not an interface, it has a purpose is to determine whether a class can traverse
SYNTAX:
1 2 3 4 | Traversable { } |
Traversable interface can not be implemented directly.Traversable important use is to determine whether a class can traverse:
1 2 3 4 5 | if ( $class instanceof Traversable) { // Foreach } |
However, Traversable interface is an empty interface, you can not implement it directly, I have used IteratorAggregate over here. You will learn about IteratorAggregate interface later on this article.
EXAMPLE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | Class Triangle implements IteratorAggregate { public $var = "Public property one"; protected function showarea() { $t=0.5 * $this->d1 * $this->d2; return $t; } // Common method public function display() { } public function getIterator() { return new ArrayIterator($this); } } $t1 = new Triangle(); if ( $t1 instanceof Traversable) { echo "yes, It is traversable"; } |
The Traversable interface is a class that can be traversed using the foreach() loop using any class implemented.So, In this example demonstrates that I have traversed the Triangle class which implements IteratorAggregate interface. This code will print if the class is traversable.
NOTE: You can use Iterator or IteratorAggregate to implement the Traversable interface.
2. Iterator Interface
Iterator interface is mainly used to allow a class to implement a basic iterative function.After you implement Iterator interface, you can iterate the instances of the class by using the foreach() loop.
Let’s see the Syntax of Iterator Interface
SYNTAX:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Iterator extends Traversable { //Return the key index of the pointer of the current element abstract public scalar key (void ) //Move the pointer to the next element abstract public void next (void ) //Return the first element or reset index cursor pointer abstract public void rewind (void ) // Determine whether the current position is valid abstract public boolean valid (void) } |
External iterator interface is kind of object which implements iterator interface and iterate interface internal data.
The following example defines an iterator interface
Example:
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 Triangle implements Iterator { private $dim1, $dim2; private $current; public function __construct($dim1, $dim2,$traverse) { $this->dim1 = $dim1; $this->dim2 = $dim2; $this->traverse = $traverse; } public function rewind() { $this->current = $traverse; } public function key() { return $this->current; } public function current() { return 0.5 * $this->dim1 * $this->dim2; } public function next() { $this->current++; $this->dim1++; $this->dim2++; } public function valid() { return $this->traverse >= $this->current; } } $obj = new Triangle(10, 10,5); foreach ($obj as $key => $value) { print "The Triangle of $key is $value<br/>"; } |
Here in above example, I traverse the simple key/value array but you can use for more complex data or for in depth traversal using this few lines of code.Here we have just overloaded foreach loop.Here class itself interact with data using the methods class have.Once you iterate the Object, the Current position is stored as an object so you can’t use nested loop.
3. IteratorAggregate interface
It is mainly used to create an external iterator interface and IteratorAggregate interface is useful when you want to go for nested loop of object as it separates the implementation of class and its iterator and class can implement the IteratorAggregate interface
To implement an IteratorAggregate interface, use the following syntax:
SYNTAX:
1 2 3 4 5 6 | IteratorAggregate extends Traversable { /*The implementation of this method return an instance of an implementation class Iterator interface */ abstract public Traversable getIterator (void) } |
The getIterator method returns a value which must be traversable or implement interface Iterator.
Here’s a simple example to understand the IteratorAggregate interface
Examples:
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 | class Triangle implements IteratorAggregate { private $dim1, $dim2; protected $return_arr; public function __construct($dim1, $dim2) { $this->dim1 = $dim1; $this->dim2 = $dim2; } public function showarea() { $this->return_arr = array(); if(is_array($this->dim1)){ for($i=0;$i<count($this->dim1);$i++){ $t=0.5 * $this->dim1[$i] * $this->dim2[$i]; array_push($this->return_arr, $t ); } } } // Common method public function display() { print $this->showarea(); } public function getIterator() { return new ArrayIterator($this->return_arr); } } $t1 = new Triangle(array(4,5),array(6,4)); $t1->display(); foreach ($t1 as $key => $value) { echo "key : $key value: $value <br/>"; } |
Here, I have modified the previous example of Iterator Interface and this function will output the values using ArrayIterator.Here, I’ve defined the method that returns an object of the class and implement the iteration.
4. ArrayAccess interface
ArrayAccess interface allows you to overload an object so that it can be treated like an array.It is an Object that implements ArrayAccess interface and use the same as an array.It allows class to overload the array syntax and implement the ArrayAccess interface.
SYNTAX:
1 2 3 4 5 6 7 8 9 | ArrayAccess { / * Methods * / abstract public boolean offsetExists (mixed $ offset) // To check if there is an offset exists abstract public mixed offsetGet (mixed $ offset) // To get an offset value abstract public void offsetUnset (mixed $ offset) // To reset an offset value of the location } |
The following example shows how to use ArrayAccess interface.
Example:
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 | Class Triangle implements ArrayAccess { private $dim1; public function __construct($dim1) { $this->container = array( $dim1 ); } // Common method public function display() { print $this->showarea(); } public function offsetSet($offset, $value) { return $this->container[$offset] = $value; } public function offsetExists($offset) { return isset($this->container[$offset]); } public function offsetUnset($offset) { return $this->container[$offset] = null; } public function offsetGet($offset) { return isset($this->container[$offset]) ? $this->container[$offset] : $this->container[$offset]; } } $triangle = new Triangle(array(4,5)); var_dump(isset($triangle["0"])); var_dump(isset($triangle["2"])); |
You can see that the object $container is used just like an array, but actually when the $triangle[“0”] is called, it will invoke the offsetGet() method, which return array offset exists or not. If not, return false.
5. Serializable
It is a serialization interface. A class that implements the interface can not be used __sleep () and __wakeup ().
SYNTAX:
1 2 3 4 5 6 7 | Serializable { / * Methods * / abstract public string serialize (void) abstract public mixed unserialize (string $serialized) } |
The class implements this interface will no longer support __sleep () and __wakeup (). Whenever, there is an instance needs to be serialized, serialize method will be called. It does not call __destruct () or have other effects, unless programmed to call this method. When data is deserialized, the class will be perceived and call the appropriate unserialize () method instead of calling __construct (). If you need to perform a standard constructor, you should be treated in this method.
Examples:
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 Triangle implements Serializable { private $dim1, $dim2; protected $return_arr; public function __construct($dim1, $dim2) { $this->dim1 = $dim1; $this->dim2 = $dim2; } public function showarea() { $this->return_arr = array(); if(is_array($this->dim1)){ for($i=0;$i<count($this->dim1);$i++){ $t=0.5 * $this->dim1[$i] * $this->dim2[$i]; array_push($this->return_arr, $t ); } } return $this->return_arr; } public function serialize () { $this->return_arr = $this->showarea(); return serialize ( $this->return_arr); } public function unserialize ( $arr ) { $this->return_arr = unserialize ( $arr ); } public function getData() { return $this->return_arr; } } $triangle = new Triangle(array(4,5),array(6,4)); $serial = serialize ( $triangle ); var_dump($serial);echo "<br/>"; $unserial = unserialize ( $serial ); print_r($unserial->getData()); |
Here, the above example serializes the passed data and again return the unserialized data using the getData function and you can pass serialized data using this serialize interface easily.
6. Closure
The closure is used to create anonymous functions.Anonymous feature was introduced in PHP 5.3. The closure is the object-oriented way to use the anonymous function.
SYNTAX:
1 2 3 4 5 6 7 8 9 10 11 12 | Closure { / * Methods * / private __construct (void) public Closure bindTo ( object $newthis [, mixed $newscope = "static" ] ) public mixed call ( object $newthis [, mixed $... ] ) } |
Have you ever used predefined interface? Are you satisfied with the explanation over here? Let me know in the comments.
Comments (1)