Back to Top

What is Predefined Interface in PHP

what-is-predefined-interface-in-php

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:

Traversable interface can not be implemented directly.Traversable important use is to determine whether a class can traverse:

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:

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:

External iterator interface is kind of object which implements iterator interface and iterate interface internal data.

The following example defines an iterator interface

Example:

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:

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:

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:

The following example shows how to use ArrayAccess interface.

Example:

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:

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:

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:

Have you ever used predefined interface? Are you satisfied with the explanation over here? Let me know in the comments.

Comments (1)

  1. yes, it good! Thank you for the tutorial.It clear my doubt on Predefined Interface. Very easily understandable examples.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

What is JavaScript Hoisting

Posted on 8 years ago

Bhumi

How to use CONST keyword in PHP

Posted on 12 years ago

Bhumi