PHP | Predefined classes and Interfaces
There are classes and interfaces that can be either user defined or pre-defines in PHP language.
There are 9 predefined classes and interfaces in PHP:
Traversable: There are no methods in the Traversable interface and acts only as a base for other predefined Traversable classes. By using foreach construct and this interface, it is easily detectable whether the class is traversable.
Iterator: This is a predefined interface for objects or external iterators that are capable of internally iterating themselves. This interface extends the interface Traversable. The five methods in this interface are:
|Method name||parameters||return value||description|
|current||void||mixed||The present value is returned.|
|key||void||scalar||The present value’s key is returned.|
|next||void||void||Moving forward to the next element.|
Set the position of the file pointer to the beginning of the file.
|valid||void||bool||Check whether an array contains more entries or not.|
IteratorAggregate: This interface is an extension of Traversable interface and has one method.
|method name||parameters||return type||description|
|getIterator||void||traversable||This function is used for retrieving an iterator(external).|
This feature is available in PHP version 7. All objects that are thrown through the throw statement has an interface which acts as base interface called Throwtable. Exception and Error classes use this interface.
|Method name||number of arguments||name of arguments||return type||description|
Returns the message to be displayed when exception occurs.
|getPrevious ||0||void||throwtable||Previous exceptions are returned.|
|getCode ||0||void||mixed||The code for exception generated.|
|getFile||0||void||string||The file in which exception occurs is returned.|
|getLine ||0||void||int||The line number in which exception occurs returned.|
|getTrace||0||void||array||The stack trace is returned.|
|getTraceAsString||0||void||string||The stack trace is returned in the form of string.|
|__toString||0||void||string||The exception generated is returned in the form of string.|
ArrayAccess: This interface allows objects to be accessed as an arrays. It has 4 methods:
|offsetExists||offset value(mixed)||bool||Checks existence of the offset.|
|offsetGet||offset value(mixed)||mixed||The offset is retrieved through this method.|
|offsetSet||offset value(mixed), element(mixed)||void||A mentioned offset is given some value.|
|offsetUnset||offset value(mixed)||void||The value of offset is unset.|
Serializable: This class allows serialization in a customized fashion. If serialization of an instance is needed then the method serialize() is called. If it is written in the code inside some method there are no alterations or side effects to the program. There are two methods in this interface:
|serialize||void||string||Object is converted into string.|
|unserialize||serialized string (string)||void||The object that was converted into string is converted back to an object.|
Closure: There are some functions called anonymous function in PHP that have no name. The callback parameters are assigned the value as anonymous function. There are 5 methods:
|__construct||void||void||A constructor disallowing instantiation|
|closure||A closure can be duplicated by the class scope and mentioned particular bound object.|
|bindTo||newthis (object)closure||closure||A closure can be duplicated by the class scope and new bound object.|
| call||newthis (object)closure||mixed||The closure is binded and called.|
|fromCallable ||Callable (Callable )||closure||The callable is converted into closure.|
Generator: This interface is extension of Iterator and has 9 methods:
|current|| void|| mixed|| The yielded element can be found.|
|getReturn|| void|| mixed|| The generator’s value is returned by the function.|
|key|| void|| mixed|| The key which was yielded is returned.|
| next|| void|| void|| The generator’s execution is resumed.|
| rewind|| void|| void|| The iterator is rewinded.|
| send|| value(mixed)|| mixed|| A value is sent to the generator.|
| throw|| exception(throwtable)|| mixed|| An exception is thrown into the generator.|
| valid|| void|| bool||Verifies if there was a closing of the iterator.|
| __wakeup|| void|| void|| Callback is serialized.|