|
Predefined Interfaces and ClassesTable of Contents
See also the SPL Interfaces and reserved classes. The Traversable interfaceIntroductionInterface to detect if a class is traversable using foreach. Abstract base interface that cannot be implemented alone. Instead, it must be implemented by either IteratorAggregate or Iterator. Interface synopsis
Traversable
This interface has no methods, its only purpose is to be the base interface for all traversable classes. Changelog
Notes
The Iterator interfaceIntroductionInterface for external iterators or objects that can be iterated themselves internally. Interface synopsis
Iterator
extends
Traversable
/* Methods */
public mixed Iterator::current()
public mixed Iterator::key()
public void Iterator::next()
public void Iterator::rewind()
public bool Iterator::valid()
Predefined iteratorsPHP already provides a number of iterators for many day to day tasks. See SPL iterators for a list. ExamplesExample #1 Basic usage This example demonstrates in which order methods are called when using foreach with an iterator.
<?php The above example will output something similar to: string(18) "myIterator::rewind" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" int(0) string(12) "firstelement" string(16) "myIterator::next" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" int(1) string(13) "secondelement" string(16) "myIterator::next" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" int(2) string(11) "lastelement" string(16) "myIterator::next" string(17) "myIterator::valid" See AlsoSee also object iteration. The IteratorAggregate interfaceIntroductionInterface to create an external Iterator. Interface synopsis
IteratorAggregate
extends
Traversable
/* Methods */
public Traversable IteratorAggregate::getIterator()
ExamplesExample #1 Basic usage
<?php The above example will output something similar to: string(9) "property1" string(19) "Public property one" string(9) "property2" string(19) "Public property two" string(9) "property3" string(21) "Public property three" string(9) "property4" string(13) "last property" The InternalIterator classIntroductionClass to ease implementing IteratorAggregate for internal classes. Class synopsis
final
InternalIterator
implements
Iterator
/* Methods */
private __construct()
public mixed current()
public mixed key()
public void next()
public void rewind()
public bool valid()
ThrowableIntroduction
Throwable is the base interface for any object that
can be thrown via a
Interface synopsis
Throwable
extends
Stringable
/* Methods */
public string Throwable::getMessage()
public int Throwable::getCode()
public string Throwable::getFile()
public int Throwable::getLine()
public array Throwable::getTrace()
public string Throwable::getTraceAsString()
public Throwablenull Throwable::getPrevious()
public string Throwable::__toString()
/* Inherited methods */
public string Stringable::__toString()
Changelog
The ArrayAccess interfaceIntroductionInterface to provide accessing objects as arrays. Interface synopsis
ArrayAccess
/* Methods */
public bool ArrayAccess::offsetExists(mixed
$offset )public mixed ArrayAccess::offsetGet(mixed
$offset )public void ArrayAccess::offsetSet(mixed
$offset , mixed $value )public void ArrayAccess::offsetUnset(mixed
$offset )ExamplesExample #1 Basic usage
<?php The above example will output something similar to: bool(true) int(2) bool(false) string(7) "A value" obj Object ( [container:obj:private] => Array ( [one] => 1 [three] => 3 [two] => A value [0] => Append 1 [1] => Append 2 [2] => Append 3 ) ) The Serializable interfaceIntroductionInterface for customized serializing. Classes that implement this interface no longer support __sleep() and __wakeup(). The method serialize is called whenever an instance needs to be serialized. This does not invoke __destruct() or have any other side effect unless programmed inside the method. When the data is unserialized the class is known and the appropriate unserialize() method is called as a constructor instead of calling __construct(). If you need to execute the standard constructor you may do so in the method. Warning
As of PHP 8.1.0, a class which implements Serializable without also implementing __serialize() and __unserialize() will generate a deprecation warning. Interface synopsis
Serializable
/* Methods */
public stringnull Serializable::serialize()
public void Serializable::unserialize(string
$data )ExamplesExample #1 Basic usage
<?php The above example will output something similar to: string(38) "C:3:"obj":23:{s:15:"My private data";}" string(15) "My private data" The Closure classIntroductionClass used to represent anonymous functions. Anonymous functions yield objects of this type. This class has methods that allow further control of the anonymous function after it has been created.
Besides the methods listed here, this class also has an
Class synopsis
final
Closure
/* Methods */
private __construct()
public static Closurenull bind(Closure
$closure , objectnull $newThis , objectstringnull $newScope = "static")public Closurenull bindTo(objectnull
$newThis , objectstringnull $newScope = "static")public mixed call(object
$newThis , mixed ...$args )public static Closure fromCallable(callable
$callback )The stdClass classIntroductionA generic empty class with dynamic properties. Objects of this class can be instantiated with new operator or created by typecasting to object. Several PHP functions also create instances of this class, e.g. json_decode, mysqli_fetch_object or PDOStatement::fetchObject.
Despite not implementing
__get()/__set()
magic methods, this class allows dynamic properties and does not require the
This is not a base class as PHP does not have a concept of a universal base class. However, it is possible to create a custom class that extends from stdClass and as a result inherits the functionality of dynamic properties. Class synopsis
stdClass
This class has no methods or default properties. ExamplesExample #1 Created as a result of typecasting to object
<?php The above example will output: object(stdClass)#1 (1) { ["foo"]=> string(3) "bar" } Example #2 Created as a result of json_decode
<?php The above example will output: object(stdClass)#1 (1) { ["foo"]=> string(3) "bar" } Example #3 Declaring dynamic properties
<?php The above example will output: object(stdClass)#1 (2) { ["foo"]=> int(42) ["1"]=> int(42) } The Generator classIntroductionGenerator objects are returned from generators. Caution
Generator objects cannot be instantiated via new. Class synopsis
final
Generator
implements
Iterator
/* Methods */
public mixed current()
public mixed getReturn()
public mixed key()
public void next()
public void rewind()
public mixed send(mixed
$value )public mixed throw(Throwable
$exception )public bool valid()
public void __wakeup()
See AlsoSee also object iteration. The Fiber classIntroductionFibers represent full-stack, interruptible functions. Fibers may be suspended from anywhere in the call-stack, pausing execution within the fiber until the fiber is resumed at a later time. Class synopsis
final
Fiber
/* Methods */
public __construct(callable
$callback )public mixed start(mixed
...$args )public mixed resume(mixed
$value = null )public mixed throw(Throwable
$exception )public mixed getReturn()
public bool isStarted()
public bool isSuspended()
public bool isRunning()
public bool isTerminated()
public static mixed suspend(mixed
$value = null )public static Fibernull getCurrent()
See AlsoThe WeakReference classIntroductionWeak references allow the programmer to retain a reference to an object which does not prevent the object from being destroyed. They are useful for implementing cache like structures. WeakReferences cannot be serialized. Class synopsis
final
WeakReference
/* Methods */
public __construct()
public static WeakReference create(object
$object )public objectnull get()
WeakReference Examples
Example #1 Basic WeakReference Usage
<?php The above example will output something similar to: object(stdClass)#1 (0) { } NULL The WeakMap classIntroductionA WeakMap is map (or dictionary) that accepts objects as keys. However, unlike the otherwise similar SplObjectStorage, an object in a key of WeakMap does not contribute toward the object's reference count. That is, if at any point the only remaining reference to an object is the key of a WeakMap, the object will be garbage collected and removed from the WeakMap. Its primary use case is for building caches of data derived from an object that do not need to live longer than the object. WeakMap implements ArrayAccess, Traversable (via IteratorAggregate), and Countable, so in most cases it can be used in the same fashion as an associative array. Class synopsis
final
WeakMap
implements
ArrayAccess
Countable
IteratorAggregate
/* Methods */
public int count()
public Iterator getIterator()
public bool offsetExists(object
$object )public mixed offsetGet(object
$object )public void offsetSet(object
$object , mixed $value )public void offsetUnset(object
$object )Examples
Example #1 Weakmap usage example
<?php The above example will output: int(1) Unsetting... Dead! Done int(0) The Stringable interfaceIntroductionThe Stringable interface denotes a class as having a __toString() method. Unlike most interfaces, Stringable is implicitly present on any class that has the magic __toString() method defined, although it can and should be declared explicitly.
Its primary value is to allow functions to type check against the union
type Interface synopsis
Stringable
/* Methods */
public string Stringable::__toString()
Stringable Examples
Example #1 Basic Stringable Usage
<?php The above example will output something similar to: 123.234.42.9 The UnitEnum interfaceIntroductionThe UnitEnum interface is automatically applied to all enumerations by the engine. It may not be implemented by user-defined classes. Enumerations may not override its methods, as default implementations are provided by the engine. It is available only for type checks. Interface synopsis
UnitEnum
/* Methods */
public static array UnitEnum::cases()
The BackedEnum interfaceIntroductionThe BackedEnum interface is automatically applied to backed enumerations by the engine. It may not be implemented by user-defined classes. Enumerations may not override its methods, as default implementations are provided by the engine. It is available only for type checks. Interface synopsis
BackedEnum
extends
UnitEnum
/* Methods */
public static static BackedEnum::from(intstring
$value )public static staticnull BackedEnum::tryFrom(intstring
$value )/* Inherited methods */
public static array UnitEnum::cases()
The SensitiveParameterValue classIntroductionThe SensitiveParameterValue class allows wrapping sensitive values to protect them against accidental exposure. Values of parameters having the SensitiveParameter attribute will automatically be wrapped inside of a SensitiveParameterValue object within stack traces. Class synopsis
final
SensitiveParameterValue
/* Properties */
private
readonly
mixed
$value;
/* Methods */
public __construct(mixed
$value )public array __debugInfo()
public mixed getValue()
Properties
|