php-src/ext/spl/spl.php

661 lines
16 KiB
PHP
Raw Normal View History

2003-05-01 23:28:28 +00:00
<?php
2004-11-01 17:05:45 +00:00
/** @file spl.php
* @ingroup SPL
* @brief Documentation of internal classes and interfaces
*
* SPL - Standard PHP Library
*
2005-02-08 19:05:25 +00:00
* (c) Marcus Boerger, 2003 - 2005
*/
2004-11-01 17:05:45 +00:00
/** @mainpage SPL - Standard PHP Library
2004-05-06 22:55:25 +00:00
*
* SPL - Standard PHP Library
2003-05-01 23:28:28 +00:00
*
2004-11-01 15:50:25 +00:00
* SPL is a collection of interfaces and classes that are meant to solve
2004-11-01 18:01:06 +00:00
* standard problems and implements some efficient data access interfaces
* and classes. You'll find the classes documented using php code in the
* file spl.php or in corresponding .inc files in subdirectories examples
* and internal. Based on the internal implementations or the files in the
* examples subdirectory there are also some .php files to experiment with.
*
* The .inc files are not included automatically because they are sooner or
* later integrated into the extension. That means that you either need to
* put the code of examples/autoload.inc into your autoprepend file or that
* you have to point your ini setting auto_prepend_file to that file.
*
* Below is a list of interfaces/classes already availabel natively through
* the SPL extension grouped by category.
*
* 1) Iterators
*
* SPL offers some advanced iterator algorithms:
*
* - interface RecursiveIterator implements Iterator
* - interface OuterIterator extends Iterator
* - class RecursiveIteratorIterator implements OuterIterator
* - abstract class FilterIterator implements OuterIterator
* - class ParentIterator extends FilterIterator implements RecursiveIterator
* - interface SeekableIterator implements Iterator
* - class LimitIterator implements OuterIterator
* - class CachingIterator implements OuterIterator
* - class CachingRecursiveIterator extends CachingIterator implements RecursiveIterator
* - class IteratorIterator implements OuterIterator
* - class NoRewindIterator implements OuterIterator
* - class EmptyIterator implements Iterator
* - class InfiniteIterator extends IteratorIterator
* - class AppendIterator implements OuterIterator
*
* 2) Directories
*
* SPL offers two advanced directory classes:
*
* - class DirectoryIterator implements Iterator
* - class RecursiveDirectoryIterator extends DirectoryIterator implements RecursiveIterator
*
* 3) XML
*
* SPL offers an advanced XML handling class:
*
* - class SimpleXMLIterator extends simplexml_element extends recursiveiterator
*
* 4) Array Overloading
*
* SPL offers advanced Array overloading:
*
* - class ArrayObject implements IteratorAggregate
* - class ArrayIterator implements Iterator
*
* 5) Counting
*
2005-02-08 19:05:25 +00:00
* - interface Countable allows to hook into the standard array function count().
2004-11-01 18:01:06 +00:00
*
* 6) Exceptions
*
* SPL provides a set of standard exception classes each meant to indicate a
* certain problem type.
*
* - class LogicException extends Exception
* - class BadFunctionCallException extends LogicException
* - class BadMethodCallException extends BadFunctionCallException
* - class DomainException extends LogicException
* - class InvalidArgumentException extends LogicException
* - class LengthException extends LogicException
* - class OutOfRangeException extends LogicException
* - class RuntimeException extends Exception
* - class OutOfBoundsException extends RuntimeException
* - class OverflowException extends RuntimeException
* - class RangeException extends RuntimeException
* - class UnderflowException extends RuntimeException
*
* As the above suggest an ArrayObject creates an ArrayIterator when it comes to
* iteration (e.g. ArrayObject instance used inside foreach).
*
* A nice article about SPL can be found
2004-11-01 15:50:25 +00:00
* <a href="http://www.sitepoint.com/article/php5-standard-library/1">here</a>.
*
* You can download this documentation as a chm file
* <a href="http://php.net/~helly/php/ext/spl/spl.chm">here</a>.
*
2005-02-08 19:05:25 +00:00
* (c) Marcus Boerger, 2003 - 2005
2003-05-01 23:28:28 +00:00
*/
2004-11-01 15:50:25 +00:00
/** @defgroup ZendEngine Zend engine classes
*
* The classes and interfaces in this group are contained in the c-code of
* PHP's Zend engine.
*/
/** @defgroup SPL Internal classes
2004-05-08 12:26:28 +00:00
*
* The classes and interfaces in this group are contained in the c-code of
* ext/SPL.
*/
2004-11-01 15:50:25 +00:00
/** @defgroup Examples Example classes
2004-05-08 12:26:28 +00:00
*
* The classes and interfaces in this group are contained as PHP code in the
* examples subdirectory of ext/SPL. Sooner or later they will be moved to
* c-code.
*/
2004-11-01 15:50:25 +00:00
/** @ingroup ZendEngine
* @brief Basic Exception class.
*/
class Exception
{
/** The exception message */
protected $message;
/** The string represenations as generated during construction */
private $string;
/** The code passed to the constructor */
protected $code;
/** The file name where the exception was instantiated */
protected $file;
/** The line number where the exception was instantiated */
protected $line;
/** The stack trace */
private $trace;
/** Prevent clone
*/
2004-11-01 16:31:19 +00:00
final private function __clone() {}
2004-11-01 15:50:25 +00:00
2004-11-01 16:31:19 +00:00
/** Construct an exception
*
* @param $message Some text describing the exception
* @param $code Some code describing the exception
2004-11-01 15:50:25 +00:00
*/
2004-11-01 16:31:19 +00:00
function __construct($message = NULL, $code = 0) {
if (func_num_args()) {
$this->message = $message;
}
$this->code = $code;
$this->file = __FILE__; // of throw clause
$this->line = __LINE__; // of throw clause
$this->trace = debug_backtrace();
$this->string = StringFormat($this);
}
2004-11-01 15:50:25 +00:00
/** @return the message passed to the constructor
*/
2004-11-01 16:31:19 +00:00
final public function getMessage()
{
return $this->message;
}
2004-11-01 15:50:25 +00:00
/** @return the code passed to the constructor
*/
2004-11-01 16:31:19 +00:00
final public function getCode()
{
return $this->code;
}
2004-11-01 15:50:25 +00:00
/** @return the name of the file where the exception was thrown
*/
2004-11-01 16:31:19 +00:00
final public function getFile()
{
return $this->file;
}
2004-11-01 15:50:25 +00:00
/** @return the line number where the exception was thrown
*/
2004-11-01 16:31:19 +00:00
final public function getLine()
{
return $this->line;
}
2004-11-01 15:50:25 +00:00
/** @return the stack trace as array
*/
2004-11-01 16:31:19 +00:00
final public function getTrace()
{
return $this->trace;
}
2004-11-01 15:50:25 +00:00
/** @return the stack trace as string
*/
2004-11-01 16:31:19 +00:00
final public function getTraceAsString()
{
}
2004-11-01 15:50:25 +00:00
/** @return string represenation of exception
*/
2004-11-01 16:31:19 +00:00
public function __toString()
{
return $this->string;
}
2004-11-01 15:50:25 +00:00
}
/** @ingroup SPL
* @brief Exception that represents error in the program logic.
*
* This kind of exceptions should directly leed to a fix in your code.
*/
class LogicException extends Exception
{
}
/** @ingroup SPL
* @brief Exception thrown when a function call was illegal.
*/
class BadFunctionCallException extends LogicException
{
}
/** @ingroup SPL
* @brief Exception thrown when a method call was illegal.
*/
class BadMethodCallException extends BadFunctionCallException
{
}
2004-11-01 15:50:25 +00:00
/** @ingroup SPL
* @brief Exception that denotes a value not in the valid domain was used.
*
* This kind of exception should be used to inform about domain erors in
* mathematical sense.
*/
class DomainException extends LogicException
{
}
/** @ingroup SPL
* @brief Exception that denotes invalid arguments were passed.
*/
class InvalidArgumentException extends LogicException
{
}
/** @ingroup SPL
* @brief Exception thrown when a parameter exceeds the allowed length.
*
* This can be used for strings length, array size, file size, number of
* elements read from an Iterator and so on.
*/
class LengthException extends LogicException
{
}
/** @ingroup SPL
* @brief Exception thrown when an illegal index was requested.
*
* This represents errors that should be detected at compile time.
*
* @see OutOfBoundsException
*/
class OutOfRangeException extends LogicException
{
}
/** @ingroup SPL
* @brief Exception thrown for errors that are only detectable at runtime.
*/
class RuntimeException extends Exception
{
}
/** @ingroup SPL
* @brief Exception thrown when an illegal index was requested.
*
* This represents errors that cannot be detected at compile time.
*
* @see OutOfRangeException
*/
2004-11-01 16:31:19 +00:00
class OutOfBoundsException extends RuntimeException
2004-11-01 15:50:25 +00:00
{
}
/** @ingroup SPL
* @brief Exception thrown to indicate arithmetic/buffer overflow.
*/
class OverflowException extends RuntimeException
{
}
/** @ingroup SPL
* @brief Exception thrown to indicate range errors during program execution.
*
* Normally this means there was an arithmetic error other than under/overflow.
*/
class RangeException extends RuntimeException
{
}
/** @ingroup SPL
* @brief Exception Exception thrown to indicate arithmetic/buffer underflow.
*/
class UnderflowException extends RuntimeException
{
}
2004-11-01 17:05:45 +00:00
/** @ingroup ZendEngine
* @brief Interface to override array access of objects.
2004-05-06 22:55:25 +00:00
*/
interface ArrayAccess
{
2004-11-01 17:05:45 +00:00
/** @param $offset to modify
* @param $value new value
2004-05-06 22:55:25 +00:00
*/
function offsetSet($offset, $value);
2004-11-01 17:05:45 +00:00
/** @param $offset to retrieve
* @return value at given offset
2004-05-06 22:55:25 +00:00
*/
function offsetGet($offset);
2004-11-01 17:05:45 +00:00
/** @param $offset to delete
2004-05-06 22:55:25 +00:00
*/
function offsetUnset($offset);
2004-11-01 17:05:45 +00:00
/** @param $offset to check
* @return whether the offset exists.
2004-05-06 22:55:25 +00:00
*/
function offsetExists($offset);
}
2004-11-01 17:05:45 +00:00
/** @ingroup ZendEngine
* @brief Interface to detect a class is traversable using foreach.
2004-05-08 12:26:28 +00:00
*
* Abstract base interface that cannot be implemented alone. Instead it
2003-12-04 19:39:46 +00:00
* must be implemented by either IteratorAggregate or Iterator.
2003-06-09 16:58:51 +00:00
*
2004-11-01 17:05:45 +00:00
* @note Internal classes that implement this interface can be used in a
2003-12-04 19:39:46 +00:00
* foreach construct and do not need to implement IteratorAggregate or
* Iterator.
2004-01-29 00:10:33 +00:00
*
2004-11-01 17:05:45 +00:00
* @note This is an engine internal interface which cannot be implemented
2004-05-06 21:20:50 +00:00
* in PHP scripts. Either IteratorAggregate or Iterator must be used
* instead.
2003-06-09 16:58:51 +00:00
*/
2004-01-29 00:10:33 +00:00
interface Traversable
{
2003-06-09 16:58:51 +00:00
}
2003-05-01 23:28:28 +00:00
2004-11-01 17:05:45 +00:00
/** @ingroup ZendEngine
* @brief Interface to create an external Iterator.
2004-01-29 00:10:33 +00:00
*
2004-11-01 17:05:45 +00:00
* @note This is an engine internal interface.
2003-12-04 19:39:46 +00:00
*/
interface IteratorAggregate extends Traversable
2004-01-29 00:10:33 +00:00
{
2004-11-01 17:05:45 +00:00
/** @return an Iterator for the implementing object.
2003-06-09 16:58:51 +00:00
*/
2003-12-04 19:39:46 +00:00
function getIterator();
2003-06-09 16:58:51 +00:00
}
2003-05-01 23:28:28 +00:00
2004-11-01 17:05:45 +00:00
/** @ingroup ZendEngine
* @brief Basic iterator
2004-05-08 12:26:28 +00:00
*
* Interface for external iterators or objects that can be iterated
2003-12-04 19:39:46 +00:00
* themselves internally.
2004-01-29 00:10:33 +00:00
*
2004-11-01 17:05:45 +00:00
* @note This is an engine internal interface.
2003-06-09 16:58:51 +00:00
*/
interface Iterator extends Traversable
2004-01-29 00:10:33 +00:00
{
2003-12-04 19:39:46 +00:00
/** Rewind the Iterator to the first element.
2003-06-09 16:58:51 +00:00
*/
function rewind();
2003-12-04 19:39:46 +00:00
/** Return the current element.
*/
function current();
/** Return the key of the current element.
2003-05-01 23:28:28 +00:00
*/
2003-06-09 16:58:51 +00:00
function key();
2003-05-01 23:28:28 +00:00
2003-12-04 19:39:46 +00:00
/** Move forward to next element.
*/
function next();
2003-05-01 23:28:28 +00:00
2003-12-04 19:39:46 +00:00
/** Check if there is a current element after calls to rewind() or next().
*/
function valid();
2003-06-09 16:58:51 +00:00
}
/** @ingroup SPL
* @brief This Interface allows to hook into the global count() function.
*/
interface Countable
{
/** @return the number the global function count() should show
*/
function count();
}
2004-11-01 17:05:45 +00:00
/** @ingroup SPL
* @brief An Array wrapper
*
2004-04-26 21:34:45 +00:00
* This array wrapper allows to recursively iterate over Arrays and public
* Object properties.
*
2004-11-01 17:05:45 +00:00
* @see ArrayIterator
*/
class ArrayObject implements IteratorAggregate, ArrayAccess, Countable
2004-01-29 00:10:33 +00:00
{
2003-12-04 19:39:46 +00:00
/** Construct a new array iterator from anything that has a hash table.
* That is any Array or Object.
*
2004-11-01 17:05:45 +00:00
* @param $array the array to use.
*/
function __construct($array);
2004-11-01 17:05:45 +00:00
/** @return the iterator which is an ArrayIterator object connected to
2004-04-26 21:34:45 +00:00
* this object.
*/
2003-12-04 19:39:46 +00:00
function getIterator();
2004-04-26 21:34:45 +00:00
2004-11-01 17:05:45 +00:00
/** @param $index offset to inspect
* @return whetehr offset $index esists
2004-04-26 21:34:45 +00:00
*/
function offsetExists($index);
2004-11-01 17:05:45 +00:00
/** @param $index offset to return value for
* @return value at offset $index
2004-04-26 21:34:45 +00:00
*/
function offsetGet($index);
2004-11-01 17:05:45 +00:00
/** @param $index index to set
* @param $newval new value to store at offset $index
2004-04-26 21:34:45 +00:00
*/
function offsetSet($index, $newval);
2004-11-01 17:05:45 +00:00
/** @param $index offset to unset
2004-04-26 21:34:45 +00:00
*/
function offsetUnset($index);
2004-11-01 17:05:45 +00:00
/** @param $value is appended as last element
* @warning this method cannot be called when the ArrayObject refers to
* an object.
2004-04-26 21:34:45 +00:00
*/
function append($value);
2004-11-01 17:05:45 +00:00
/** @return a \b copy of the array
* @note when the ArrayObject refers to an object then this method
* returns an array of the public properties.
2004-04-26 21:34:45 +00:00
*/
function getArrayCopy();
2004-05-06 21:20:50 +00:00
2004-11-01 17:05:45 +00:00
/** @return the number of elements in the array or the number of public
2004-05-06 21:20:50 +00:00
* properties in the object.
*/
function count();
}
2004-11-01 17:05:45 +00:00
/** @ingroup SPL
* @brief An Array iterator
*
* This iterator allows to unset and modify values and keys while iterating
* over Arrays and Objects.
*
2004-04-26 21:34:45 +00:00
* When you want to iterate over the same array multiple times you need to
* instanciate ArrayObject and let it create ArrayIterator instances that
* refer to it either by using foreach or by calling its getIterator()
* method manually.
*/
2004-11-01 17:05:45 +00:00
class ArrayIterator implements SeekableIterator, ArrayAccess, Countable
2004-01-29 00:10:33 +00:00
{
2003-12-04 19:39:46 +00:00
/** Construct a new array iterator from anything that has a hash table.
* That is any Array or Object.
*
2004-11-01 17:05:45 +00:00
* @param $array the array to use.
*/
2004-04-26 21:34:45 +00:00
public function __construct($array);
2004-11-01 17:05:45 +00:00
/** @param $index offset to inspect
* @return whetehr offset $index esists
2004-04-26 21:34:45 +00:00
*/
function offsetExists($index);
2004-11-01 17:05:45 +00:00
/** @param $index offset to return value for
* @return value at offset $index
2004-04-26 21:34:45 +00:00
*/
function offsetGet($index);
2004-11-01 17:05:45 +00:00
/** @param $index index to set
* @param $newval new value to store at offset $index
2004-04-26 21:34:45 +00:00
*/
function offsetSet($index, $newval);
2004-11-01 17:05:45 +00:00
/** @param $index offset to unset
2004-04-26 21:34:45 +00:00
*/
function offsetUnset($index);
2004-11-01 17:05:45 +00:00
/** @param $value is appended as last element
* @warning this method cannot be called when the ArrayIterator refers to
* an object.
2004-04-26 21:34:45 +00:00
*/
function append($value);
2004-11-01 17:05:45 +00:00
/** @return a \b copy of the array
* @note when the ArrayIterator refers to an object then this method
* returns an array of the public properties.
2004-04-26 21:34:45 +00:00
*/
function getArrayCopy();
2004-11-01 17:05:45 +00:00
/** @param $position offset to seek to
2004-04-26 21:34:45 +00:00
*/
function seek($position);
2003-12-04 19:39:46 +00:00
2004-11-01 17:05:45 +00:00
/** @return the number of elements in the array or the number of public
2004-05-06 21:20:50 +00:00
* properties in the object.
*/
function count();
}
2004-11-01 17:05:45 +00:00
/** @ingroup SPL
* @brief Directory iterator
*/
2004-01-29 00:10:33 +00:00
class DirectoryIterator implements Iterator
{
2003-12-04 19:39:46 +00:00
/** Construct a directory iterator from a path-string.
*
2004-11-01 17:05:45 +00:00
* @param $path directory to iterate.
*/
function __construct($path);
2004-11-01 17:05:45 +00:00
/** @return The opened path.
2003-12-04 19:39:46 +00:00
*/
function getPath();
2004-11-01 17:05:45 +00:00
/** @return The current file name.
2003-12-04 19:39:46 +00:00
*/
function getFilename();
2004-11-01 17:05:45 +00:00
/** @return The current entries path and file name.
*/
2003-12-04 19:39:46 +00:00
function getPathname();
2004-11-01 17:05:45 +00:00
/** @return The current entry's permissions.
2004-04-26 21:34:45 +00:00
*/
function getPerms();
2004-11-01 17:05:45 +00:00
/** @return The current entry's inode.
2004-04-26 21:34:45 +00:00
*/
function getInode();
2004-11-01 17:05:45 +00:00
/** @return The current entry's size in bytes .
2004-04-26 21:34:45 +00:00
*/
function getSize();
2004-11-01 17:05:45 +00:00
/** @return The current entry's owner name.
2004-04-26 21:34:45 +00:00
*/
function getOwner();
2004-11-01 17:05:45 +00:00
/** @return The current entry's group name.
2004-04-26 21:34:45 +00:00
*/
function getGroup();
2004-11-01 17:05:45 +00:00
/** @return The current entry's last access time.
2004-04-26 21:34:45 +00:00
*/
function getATime();
2004-11-01 17:05:45 +00:00
/** @return The current entry's last modification time.
2004-04-26 21:34:45 +00:00
*/
function getMTime();
2004-11-01 17:05:45 +00:00
/** @return The current entry's last change time.
2004-04-26 21:34:45 +00:00
*/
function getCTime();
2004-11-01 17:05:45 +00:00
/** @return The current entry's size in bytes .
2004-04-26 21:34:45 +00:00
*/
function getType();
2004-11-01 17:05:45 +00:00
/** @return Whether the current entry is writeable.
2004-04-26 21:34:45 +00:00
*/
function isWritable();
2004-11-01 17:05:45 +00:00
/** @return Whether the current entry is readable.
2004-04-26 21:34:45 +00:00
*/
function isReadable();
2004-11-01 17:05:45 +00:00
/** @return Whether the current entry is executable.
2004-04-26 21:34:45 +00:00
*/
function isExecutable();
2004-11-01 17:05:45 +00:00
/** @return Whether the current entry is .
2004-04-26 21:34:45 +00:00
*/
function isFile();
2004-11-01 17:05:45 +00:00
/** @return Whether the current entry is a directory.
2003-12-04 19:39:46 +00:00
*/
function isDir();
2004-11-01 17:05:45 +00:00
/** @return Whether the current entry is either '.' or '..'.
2003-12-04 19:39:46 +00:00
*/
function isDot();
2004-04-26 21:34:45 +00:00
2004-11-01 17:05:45 +00:00
/** @return whether the current entry is a link.
2004-04-26 21:34:45 +00:00
*/
function isLink();
2004-11-01 17:05:45 +00:00
/** @return getFilename()
2004-04-26 21:34:45 +00:00
*/
function __toString();
}
2003-12-04 19:39:46 +00:00
2004-11-01 17:05:45 +00:00
/** @ingroup SPL
* @brief recursive directory iterator
2003-12-04 19:39:46 +00:00
*/
2004-01-29 00:10:33 +00:00
class RecursiveDirectoryIterator extends DirectoryIterator implements RecursiveIterator
{
2004-11-01 17:05:45 +00:00
/** @return whether the current is a directory (not '.' or '..').
2003-12-04 19:39:46 +00:00
*/
function hasChildren();
2004-11-01 17:05:45 +00:00
/** @return a RecursiveDirectoryIterator for the current entry.
2003-12-04 19:39:46 +00:00
*/
function getChildren();
2004-01-29 00:10:33 +00:00
}
2004-11-01 17:05:45 +00:00
/** @ingroup SPL
* @brief recursive SimpleXML_Element iterator
2004-08-29 10:33:24 +00:00
*
2004-08-29 11:54:38 +00:00
* The SimpleXMLIterator implements the RecursiveIterator interface. This
* allows iteration over all elements using foreach or an appropriate while
* construct, just like SimpleXMLElement does. When using the foreach construct,
* you will also iterate over the subelements. For every element which
* has subelements, hasChildren() returns true. This will trigger a call to
* getChildren() which returns the iterator for that sub element.
2004-01-29 00:10:33 +00:00
*/
2004-04-26 21:34:45 +00:00
class SimpleXMLIterator extends SimpleXMLElement implements RecursiveIterator
2004-01-29 00:10:33 +00:00
{
2004-11-01 17:05:45 +00:00
/** @return whether the current node has sub nodes.
2004-01-29 00:10:33 +00:00
*/
function hasChildren();
2004-11-01 17:05:45 +00:00
/** @return a SimpleXMLIterator for the current node.
2004-01-29 00:10:33 +00:00
*/
function getChildren();
2003-12-04 19:39:46 +00:00
}
?>