|
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 synopsisTraversable
class Traversable
{
}This interface has no methods, its only purpose is to be the base interface for all traversable classes. The Iterator interfaceIntroductionInterface for external iterators or objects that can be iterated themselves internally. Interface synopsisIterator
class Iterator
extends
Traversable
{
/* Methods */
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
)
}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" The IteratorAggregate interfaceIntroductionInterface to create an external Iterator. Interface synopsisIteratorAggregate
class IteratorAggregate
extends
Traversable
{
/* Methods */
abstract public Traversable getIterator
( void
)
}Example #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" ThrowableIntroductionThrowable is the base interface for any object that can be thrown via a throw statement in PHP 7, including Error and Exception.
Interface synopsisThrowable
class Throwable
{
/* Methods */
abstract public string getMessage
( void
)
abstract public int getCode
( void
)
abstract public string getFile
( void
)
abstract public int getLine
( void
)
abstract public array getTrace
( void
)
abstract public string getTraceAsString
( void
)
abstract public Throwable getPrevious
( void
)
abstract public string __toString
( void
)
}The ArrayAccess interfaceIntroductionInterface to provide accessing objects as arrays. Interface synopsisArrayAccess
class 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
)Example #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. Note, that when an old instance of a class that implements this interface now, which had been serialized before the class implemeted the interface, is unserialized, __wakeup() is called instead of the serialize method, which might be useful for migration purposes. Interface synopsisSerializable
class Serializable
{
/* Methods */
abstract public string serialize
( void
)
abstract public void unserialize
( string
}$serialized
)Example #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, implemented in PHP 5.3, yield objects of this type. This fact used to be considered an implementation detail, but it can now be relied upon. Starting with PHP 5.4, 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 __invoke method. This is for consistency with other classes that implement calling magic, as this method is not used for calling the function. Class synopsisClosure
class Closure
{
/* Methods */
private __construct
( void
)
public static Closure bind
( Closure
$closure
, object $newthis
[, mixed $newscope
= "static"
] )
public Closure bindTo
( object
$newthis
[, mixed $newscope
= "static"
] )
public mixed call
( object
$newthis
[, mixed $...
] )
public static Closure fromCallable
( callable
}$callable
)The Generator classIntroductionGenerator objects are returned from generators. Caution
Generator objects cannot be instantiated via new. Class synopsisGenerator
class Generator
implements
Iterator
{
/* Methods */
public mixed current
( void
)
public mixed getReturn
( void
)
public mixed key
( void
)
public void next
( void
)
public void rewind
( void
)
public mixed send
( mixed
$value
)
public mixed throw
( Throwable
$exception
)
public bool valid
( void
)
public void __wakeup
( void
)
} |