Skip to content
Related Articles

Related Articles

Improve Article

PHP | Predefined classes and Interfaces

  • Last Updated : 11 Jun, 2020

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
  • Iterator 
  • IteratorAggregate
  • Throwtable
  • ArrayAccess
  • Serializable
  • Closure
  • Generator
     

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 nameparametersreturn valuedescription
currentvoidmixedThe present value is returned.
keyvoidscalarThe present value’s key is returned.
nextvoidvoidMoving forward to the next element.
rewindvoidvoid

Set the position of the file pointer to the beginning of the file.



validvoidboolCheck whether an array contains more entries or not.

IteratorAggregate: This interface is an extension of Traversable interface and has one method.

method nameparametersreturn typedescription
getIteratorvoidtraversableThis function is used for retrieving an iterator(external).

Throwtable

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 namenumber of argumentsname of argumentsreturn typedescription
getMessage0voidstring

Returns the message to be displayed when exception occurs.

getPrevious 0voidthrowtablePrevious exceptions are returned.
getCode 0voidmixedThe code for exception generated.
getFile0voidstringThe file in which exception occurs is returned.
getLine 0voidintThe line number in which exception occurs returned.
getTrace0voidarrayThe stack trace is returned.
getTraceAsString0voidstringThe stack trace is returned in the form of string.
__toString0voidstringThe exception generated is returned in the form of string.

ArrayAccess: This interface allows objects to be accessed as an arrays. It has 4 methods:

method nameparametersreturnsdescription
offsetExistsoffset value(mixed)boolChecks existence of the offset.
offsetGetoffset value(mixed)mixedThe offset is retrieved through this method.
offsetSetoffset value(mixed), element(mixed)voidA mentioned offset is given some value.
offsetUnsetoffset value(mixed)voidThe 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:

method nameparametersreturndescription
serializevoidstringObject is converted into string.
unserializeserialized string (string)voidThe 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:

method nameparametersreturnsdescription
__constructvoidvoidA constructor disallowing instantiation
bind

object (closure)

newthis (object)closure

closureA closure can be duplicated by the class scope and mentioned particular bound object.
bindTonewthis (object)closureclosureA closure can be duplicated by the class scope and new bound object.
 callnewthis (object)closuremixedThe closure is binded and called.
fromCallable Callable (Callable )closureThe callable is converted into closure.

Generator: This interface is extension of Iterator and has 9 methods:

method nameparametersreturnsdescitption
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 boolVerifies if there was a closing of the iterator.
 __wakeup void void Callback is serialized.

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :