Web Development

Predefined Interfaces in PHP

You might have seen my earlier article related to Magic Constants in PHP. Those are constants already declared by the core PHP which we can not declare in our code.

So same as these constants we have set of Interface also which are already declared by the PHP. If you are not sure what is Interface in PHP, then I would suggest to read this article related to Interface In PHP.

So I hope that you are much clear about interface in PHP after reading my article. So Today’s article is about to introduce the predefined interfaces in PHP. In this article I will list out those interfaces and later we will have full detailed articles for each of them.

So Here is the list of those Interfaces:

  1. Traversable
  2. Iterator
  3. IteratorAggregate
  4. ArrayAccess
  5. Serializable
  6. Closure

Here now we will have brief intorduction of each of the interfaces.

Traversable

Interface to detect if a class is traversable using foreach.
This interface has no methods, its only purpose is to be the base interface for all traversable classes.

Iterator

Interface for external iterators or objects that can be iterated themselves internally.

This interface provides below methods:

[cc lang=”php”]
Iterator extends Traversable
{
abstract public mixed current ( void )
abstract public scalar key ( void )
abstract public void next ( void )
abstract public void rewind ( void )
abstract public boolean valid ( void )
}
[/cc]

IteratorAggregate

Interface to create an external Iterator.

This interface can be used if you want to create an external Iterator.

It has below methods:

[cc lang=”php”]
IteratorAggregate extends Traversable
{
/* Methods */
abstract public Traversable getIterator ( void )
}
[/cc]

ArrayAccess

This interface provides a way to access objects as an arrays.

It has below methods defined:

[cc lang=”php”]
ArrayAccess
{
/* Methods */
abstract public boolean offsetExists ( mixed $offset )
abstract public mixed offsetGet ( mixed $offset )
abstract public void offsetSet ( mixed $offset , mixed $value )
abstract public void offsetUnset ( mixed $offset )
}
[/cc]

Serializable

This interface is used for customized serializing. Classes that implement this interface no longer support __sleep() and __wakeup().

[cc lang=”php”]
Serializable
{
/* Methods */
abstract public string serialize ( void )
abstract public void unserialize ( string $serialized )
}
[/cc]

Closure

Class used to represent anonymous functions. Anonymous functions, implemented in PHP 5.3. This class also has an __invoke method

[cc lang=”php”]
Closure
{
/* Methods */
__construct ( void )
public static Closure bind ( Closure $closure , object $newthis [, mixed $newscope=’static’])
public Closure bindTo ( object $newthis [, mixed $newscope = ‘static’ ] )
}
[/cc]

So here is the quick introduction all predefined interfaces, Later we will have full detailed article for each of the features. Subscribe to our RSS Feed by Email if you don’t want to miss those articles.

Shares:

Leave a Reply

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