Spl/datastructures-Phpdoc专题
数据结构
目录
- SplDoublyLinkedList
— The SplDoublyLinkedList class
- SplDoublyLinkedList::add — Add/insert a new value at the specified index
- SplDoublyLinkedList::bottom — Peeks at the node from the beginning of the doubly linked list
- SplDoublyLinkedList::__construct — Constructs a new doubly linked list
- SplDoublyLinkedList::count — Counts the number of elements in the doubly linked list
- SplDoublyLinkedList::current — Return current array entry
- SplDoublyLinkedList::getIteratorMode — Returns the mode of iteration
- SplDoublyLinkedList::isEmpty — Checks whether the doubly linked list is empty
- SplDoublyLinkedList::key — Return current node index
- SplDoublyLinkedList::next — Move to next entry
- SplDoublyLinkedList::offsetExists — Returns whether the requested $index exists
- SplDoublyLinkedList::offsetGet — Returns the value at the specified $index
- SplDoublyLinkedList::offsetSet — Sets the value at the specified $index to $newval
- SplDoublyLinkedList::offsetUnset — Unsets the value at the specified $index
- SplDoublyLinkedList::pop — Pops a node from the end of the doubly linked list
- SplDoublyLinkedList::prev — Move to previous entry
- SplDoublyLinkedList::push — Pushes an element at the end of the doubly linked list
- SplDoublyLinkedList::rewind — Rewind iterator back to the start
- SplDoublyLinkedList::serialize — Serializes the storage
- SplDoublyLinkedList::setIteratorMode — Sets the mode of iteration
- SplDoublyLinkedList::shift — Shifts a node from the beginning of the doubly linked list
- SplDoublyLinkedList::top — Peeks at the node from the end of the doubly linked list
- SplDoublyLinkedList::unserialize — Unserializes the storage
- SplDoublyLinkedList::unshift — Prepends the doubly linked list with an element
- SplDoublyLinkedList::valid — Check whether the doubly linked list contains more nodes
- SplStack — The SplStack class
- SplStack::__construct — Constructs a new stack implemented using a doubly linked list
- SplStack::setIteratorMode — Sets the mode of iteration
- SplQueue — The SplQueue class
- SplQueue::__construct — Constructs a new queue implemented using a doubly linked list
- SplQueue::dequeue — Dequeues a node from the queue
- SplQueue::enqueue — Adds an element to the queue
- SplQueue::setIteratorMode — Sets the mode of iteration
- SplHeap — The SplHeap class
- SplHeap::compare — Compare elements in order to place them correctly in the heap while sifting up
- SplHeap::__construct — Constructs a new empty heap
- SplHeap::count — Counts the number of elements in the heap
- SplHeap::current — Return current node pointed by the iterator
- SplHeap::extract — Extracts a node from top of the heap and sift up
- SplHeap::insert — Inserts an element in the heap by sifting it up
- SplHeap::isCorrupted — Tells if the heap is in a corrupted state
- SplHeap::isEmpty — Checks whether the heap is empty
- SplHeap::key — Return current node index
- SplHeap::next — Move to the next node
- SplHeap::recoverFromCorruption — Recover from the corrupted state and allow further actions on the heap
- SplHeap::rewind — Rewind iterator back to the start (no-op)
- SplHeap::top — Peeks at the node from the top of the heap
- SplHeap::valid — Check whether the heap contains more nodes
- SplMaxHeap — The SplMaxHeap
class
- SplMaxHeap::compare — Compare elements in order to place them correctly in the heap while sifting up
- SplMinHeap — The SplMinHeap
class
- SplMinHeap::compare — Compare elements in order to place them correctly in the heap while sifting up
- SplPriorityQueue — The
SplPriorityQueue class
- SplPriorityQueue::compare — Compare priorities in order to place elements correctly in the heap while sifting up
- SplPriorityQueue::__construct — Constructs a new empty queue
- SplPriorityQueue::count — Counts the number of elements in the queue
- SplPriorityQueue::current — Return current node pointed by the iterator
- SplPriorityQueue::extract — Extracts a node from top of the heap and sift up
- SplPriorityQueue::getExtractFlags — Get the flags of extraction
- SplPriorityQueue::insert — Inserts an element in the queue by sifting it up
- SplPriorityQueue::isCorrupted — Tells if the priority queue is in a corrupted state
- SplPriorityQueue::isEmpty — Checks whether the queue is empty
- SplPriorityQueue::key — Return current node index
- SplPriorityQueue::next — Move to the next node
- SplPriorityQueue::recoverFromCorruption — Recover from the corrupted state and allow further actions on the queue
- SplPriorityQueue::rewind — Rewind iterator back to the start (no-op)
- SplPriorityQueue::setExtractFlags — Sets the mode of extraction
- SplPriorityQueue::top — Peeks at the node from the top of the queue
- SplPriorityQueue::valid — Check whether the queue contains more nodes
- SplFixedArray — The
SplFixedArray class
- SplFixedArray::__construct — Constructs a new fixed array
- SplFixedArray::count — Returns the size of the array
- SplFixedArray::current — Return current array entry
- SplFixedArray::fromArray — Import a PHP array in a SplFixedArray instance
- SplFixedArray::getSize — Gets the size of the array
- SplFixedArray::key — Return current array index
- SplFixedArray::next — Move to next entry
- SplFixedArray::offsetExists — Returns whether the requested index exists
- SplFixedArray::offsetGet — Returns the value at the specified index
- SplFixedArray::offsetSet — Sets a new value at a specified index
- SplFixedArray::offsetUnset — Unsets the value at the specified $index
- SplFixedArray::rewind — Rewind iterator back to the start
- SplFixedArray::setSize — Change the size of an array
- SplFixedArray::toArray — Returns a PHP array from the fixed array
- SplFixedArray::valid — Check whether the array contains more elements
- SplFixedArray::__wakeup — Reinitialises the array after being unserialised
- SplObjectStorage — The
SplObjectStorage class
- SplObjectStorage::addAll — Adds all objects from another storage
- SplObjectStorage::attach — Adds an object in the storage
- SplObjectStorage::contains — Checks if the storage contains a specific object
- SplObjectStorage::count — Returns the number of objects in the storage
- SplObjectStorage::current — Returns the current storage entry
- SplObjectStorage::detach — Removes an object from the storage
- SplObjectStorage::getHash — Calculate a unique identifier for the contained objects
- SplObjectStorage::getInfo — Returns the data associated with the current iterator entry
- SplObjectStorage::key — Returns the index at which the iterator currently is
- SplObjectStorage::next — Move to the next entry
- SplObjectStorage::offsetExists — Checks whether an object exists in the storage
- SplObjectStorage::offsetGet — Returns the data associated with an object
- SplObjectStorage::offsetSet — Associates data to an object in the storage
- SplObjectStorage::offsetUnset — Removes an object from the storage
- SplObjectStorage::removeAll — Removes objects contained in another storage from the current storage
- SplObjectStorage::removeAllExcept — Removes all objects except for those contained in another storage from the current storage
- SplObjectStorage::rewind — Rewind the iterator to the first storage element
- SplObjectStorage::serialize — Serializes the storage
- SplObjectStorage::setInfo — Sets the data associated with the current iterator entry
- SplObjectStorage::unserialize — Unserializes a storage from its string representation
- SplObjectStorage::valid — Returns if the current iterator entry is valid
SPL 提供了一套标准的数据结构。它们按底层实现进行分组, 通常定义了它们的一般应用领域。
双向链表
双链表 (DLL) 是一个链接到两个方向的节点列表。当底层结构是 DLL 时, 迭代器的操作、对两端的访问、节点的添加或删除都具有 O (1) 的开销。因此, 它为栈和队列提供了一个合适的实现。
- <span
class="classname">SplDoublyLinkedList
- <span class="classname">SplStack
- <span class="classname">SplQueue
堆
堆是遵循堆属性的树状结构: 每个节点都大于或等于其子级, 使用对堆全局的已实现的比较方法进行比较。
- SplHeap
- <span class="classname">SplMaxHeap
- <span class="classname">SplMinHeap
- <span class="classname">SplPriorityQueue
数组
数组是以连续方式存储数据的结构, 可通过索引进行访问。不要将它们与 php 数组混淆: php 数组实际上是按照有序的列表实现的。
- <span class="classname">SplFixedArray
映射
映射是一个数据拥有键值对。PHP 数组可以被看作是从整数/字符串到值的映射。SPL 提供了从对象到数据的映射。此映射也可用作对象集。
- <span class="classname">SplObjectStorage
简介
The SplDoublyLinkedList class provides the main functionalities of a doubly linked list.
类摘要
SplDoublyLinkedList
class SplDoublyLinkedList <span class="oointerface">implements <span class="interfacename">Iterator <span class="oointerface">, ArrayAccess , <span class="interfacename">Countable <span class="oointerface">, Serializable {
/* 常量 */
const int
SplDoublyLinkedList::IT_MODE_LIFO =
2 ;
const int
SplDoublyLinkedList::IT_MODE_FIFO =
0 ;
const int
SplDoublyLinkedList::IT_MODE_DELETE =
1 ;
const int
SplDoublyLinkedList::IT_MODE_KEEP =
0 ;
/* 方法 */
public <span class="methodname">__construct ( <span class="methodparam">void )
public void
add ( <span
class="type">mixed $index , <span
class="methodparam">mixed $newval )
public mixed bottom ( <span class="methodparam">void )
public int <span class="methodname">count ( void )
public mixed current ( <span class="methodparam">void )
public int <span class="methodname">getIteratorMode ( <span class="methodparam">void )
public bool isEmpty ( <span class="methodparam">void )
public mixed key ( <span class="methodparam">void )
public void next ( <span class="methodparam">void )
public bool
offsetExists ( <span
class="methodparam">mixed $index )
public mixed
offsetGet ( <span
class="methodparam">mixed $index )
public void
offsetSet ( <span
class="methodparam">mixed $index ,
mixed
$newval )
public void
offsetUnset ( <span
class="methodparam">mixed $index )
public mixed pop ( <span class="methodparam">void )
public void prev ( <span class="methodparam">void )
public void
push ( <span
class="type">mixed $value )
public void rewind ( <span class="methodparam">void )
public string serialize ( <span class="methodparam">void )
public void
setIteratorMode ( <span
class="methodparam">int $mode )
public mixed shift ( <span class="methodparam">void )
public mixed top ( <span class="methodparam">void )
public void
unserialize ( <span
class="methodparam">string
$serialized )
public void
unshift ( <span
class="methodparam">mixed $value )
public bool valid ( <span class="methodparam">void )
}
预定义常量
Iteration Direction
SplDoublyLinkedList::IT_MODE_LIFO
The list will be iterated in a last in, first out order, like a stack.
SplDoublyLinkedList::IT_MODE_FIFO
The list will be iterated in a first in, first out order, like a queue.
Iteration Behavior
SplDoublyLinkedList::IT_MODE_DELETE
Iteration will remove the iterated elements.
SplDoublyLinkedList::IT_MODE_KEEP
Iteration will not remove the iterated elements.
SplDoublyLinkedList::add
Add/insert a new value at the specified index
说明
public void
SplDoublyLinkedList::add ( <span
class="methodparam">mixed $index ,
mixed
$newval )
Insert the value newval at the specified index, shuffling the
previous value at that index (and all subsequent values) up through the
list.
参数
index
The index where the new value is to be inserted.
newval
The new value for the index.
返回值
没有返回值。
错误/异常
Throws OutOfRangeException when index
is out of bounds or when index cannot be parsed as an integer.
SplDoublyLinkedList::bottom
Peeks at the node from the beginning of the doubly linked list
说明
public mixed SplDoublyLinkedList::bottom ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
The value of the first node.
错误/异常
Throws RuntimeException when the data-structure is empty.
SplDoublyLinkedList::__construct
Constructs a new doubly linked list
说明
public <span class="methodname">SplDoublyLinkedList::__construct ( <span class="methodparam">void )
This constructs a new empty doubly linked list.
参数
此函数没有参数。
返回值
没有返回值。
范例
示例 #1 <span class="function">SplDoublyLinkedList::__construct example
<?php
$dll = new SplDoublyLinkedList();
$dll->push(2);
$dll->push(3);
$dll->unshift(5);
var_dump($dll);
?>
以上例程会输出:
object(SplDoublyLinkedList)#1 (2) {
["flags":"SplDoublyLinkedList":private]=>
int(0)
["dllist":"SplDoublyLinkedList":private]=>
array(3) {
[0]=>
int(5)
[1]=>
int(2)
[2]=>
int(3)
}
}
SplDoublyLinkedList::count
Counts the number of elements in the doubly linked list
说明
public int <span class="methodname">SplDoublyLinkedList::count ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
Returns the number of elements in the doubly linked list.
SplDoublyLinkedList::current
Return current array entry
说明
public mixed SplDoublyLinkedList::current ( <span class="methodparam">void )
Get the current doubly linked list node.
参数
此函数没有参数。
返回值
The current node value.
SplDoublyLinkedList::getIteratorMode
Returns the mode of iteration
说明
public int <span class="methodname">SplDoublyLinkedList::getIteratorMode ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
Returns the different modes and flags that affect the iteration.
SplDoublyLinkedList::isEmpty
Checks whether the doubly linked list is empty
说明
public bool SplDoublyLinkedList::isEmpty ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
Returns whether the doubly linked list is empty.
SplDoublyLinkedList::key
Return current node index
说明
public mixed SplDoublyLinkedList::key ( <span class="methodparam">void )
This function returns the current node index
参数
此函数没有参数。
返回值
The current node index.
SplDoublyLinkedList::next
Move to next entry
说明
public void SplDoublyLinkedList::next ( <span class="methodparam">void )
Move the iterator to the next node.
参数
此函数没有参数。
返回值
没有返回值。
SplDoublyLinkedList::offsetExists
Returns whether the requested $index exists
说明
public bool
SplDoublyLinkedList::offsetExists (
mixed
$index )
参数
index
The index being checked.
返回值
true if the requested index exists, otherwise false
SplDoublyLinkedList::offsetGet
Returns the value at the specified $index
说明
public mixed
SplDoublyLinkedList::offsetGet ( <span
class="methodparam">mixed $index )
参数
index
The index with the value.
返回值
The value at the specified index.
错误/异常
Throws OutOfRangeException when index
is out of bounds or when index cannot be parsed as an integer.
SplDoublyLinkedList::offsetSet
Sets the value at the specified $index to $newval
说明
public void
SplDoublyLinkedList::offsetSet ( <span
class="methodparam">mixed $index ,
mixed
$newval )
Sets the value at the specified index to newval.
参数
index
The index being set.
newval
The new value for the index.
返回值
没有返回值。
错误/异常
Throws OutOfRangeException when index
is out of bounds or when index cannot be parsed as an integer.
SplDoublyLinkedList::offsetUnset
Unsets the value at the specified $index
说明
public void
SplDoublyLinkedList::offsetUnset ( <span
class="methodparam">mixed $index )
Unsets the value at the specified index.
参数
index
The index being unset.
返回值
没有返回值。
错误/异常
Throws OutOfRangeException when index
is out of bounds or when index cannot be parsed as an integer.
SplDoublyLinkedList::pop
Pops a node from the end of the doubly linked list
说明
public mixed SplDoublyLinkedList::pop ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
The value of the popped node.
错误/异常
Throws RuntimeException when the data-structure is empty.
SplDoublyLinkedList::prev
Move to previous entry
说明
public void SplDoublyLinkedList::prev ( <span class="methodparam">void )
Move the iterator to the previous node.
参数
此函数没有参数。
返回值
没有返回值。
SplDoublyLinkedList::push
Pushes an element at the end of the doubly linked list
说明
public void
SplDoublyLinkedList::push ( <span
class="methodparam">mixed $value )
Pushes value at the end of the doubly linked list.
参数
value
The value to push.
返回值
没有返回值。
SplDoublyLinkedList::rewind
Rewind iterator back to the start
说明
public void SplDoublyLinkedList::rewind ( <span class="methodparam">void )
This rewinds the iterator to the beginning.
参数
此函数没有参数。
返回值
没有返回值。
SplDoublyLinkedList::serialize
Serializes the storage
说明
public string SplDoublyLinkedList::serialize ( <span class="methodparam">void )
Serializes the storage.
Warning
本函数还未编写文档,仅有参数列表。
参数
此函数没有参数。
返回值
The serialized string.
参见
- SplDoublyLinkedList::unserialize
SplDoublyLinkedList::setIteratorMode
Sets the mode of iteration
说明
public void
SplDoublyLinkedList::setIteratorMode (
int $mode )
参数
mode
There are two orthogonal sets of modes that can be set:
- The direction of the iteration (either one or
the other):
SplDoublyLinkedList::IT_MODE_LIFO(Stack style)SplDoublyLinkedList::IT_MODE_FIFO(Queue style)
- The behavior of the iterator (either one or
the other):
SplDoublyLinkedList::IT_MODE_DELETE(Elements are deleted by the iterator)SplDoublyLinkedList::IT_MODE_KEEP(Elements are traversed by the iterator)
The default mode is: SplDoublyLinkedList::IT_MODE_FIFO |
SplDoublyLinkedList::IT_MODE_KEEP
返回值
没有返回值。
SplDoublyLinkedList::shift
Shifts a node from the beginning of the doubly linked list
说明
public mixed SplDoublyLinkedList::shift ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
The value of the shifted node.
错误/异常
Throws RuntimeException when the data-structure is empty.
SplDoublyLinkedList::top
Peeks at the node from the end of the doubly linked list
说明
public mixed SplDoublyLinkedList::top ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
The value of the last node.
错误/异常
Throws RuntimeException when the data-structure is empty.
SplDoublyLinkedList::unserialize
Unserializes the storage
说明
public void
SplDoublyLinkedList::unserialize ( <span
class="methodparam">string
$serialized )
Unserializes the storage, from <span class="methodname">SplDoublyLinkedList::serialize.
Warning
本函数还未编写文档,仅有参数列表。
参数
serialized
The serialized string.
返回值
没有返回值。
参见
- SplDoublyLinkedList::serialize
SplDoublyLinkedList::unshift
Prepends the doubly linked list with an element
说明
public void
SplDoublyLinkedList::unshift ( <span
class="methodparam">mixed $value )
Prepends value at the beginning of the doubly linked list.
参数
value
The value to unshift.
返回值
没有返回值。
SplDoublyLinkedList::valid
Check whether the doubly linked list contains more nodes
说明
public bool SplDoublyLinkedList::valid ( <span class="methodparam">void )
Checks if the doubly linked list contains any more nodes.
参数
此函数没有参数。
返回值
Returns true if the doubly linked list contains any more nodes,
false otherwise.
简介
SplStack类通过使用一个双向链表来提供栈的主要功能。
类摘要
SplStack
class SplStack extends SplDoublyLinkedList implements <span class="interfacename">Iterator <span class="oointerface">, ArrayAccess , <span class="interfacename">Countable {
/* 方法 */
__construct ( <span class="methodparam">void )
void <span
class="methodname">setIteratorMode ( <span
class="methodparam">int $mode )
/* 继承的方法 */
public void
SplDoublyLinkedList::add ( <span
class="methodparam">mixed $index ,
mixed
$newval )
public mixed SplDoublyLinkedList::bottom ( <span class="methodparam">void )
public int <span class="methodname">SplDoublyLinkedList::count ( <span class="methodparam">void )
public mixed SplDoublyLinkedList::current ( <span class="methodparam">void )
public int <span class="methodname">SplDoublyLinkedList::getIteratorMode ( <span class="methodparam">void )
public bool SplDoublyLinkedList::isEmpty ( <span class="methodparam">void )
public mixed SplDoublyLinkedList::key ( <span class="methodparam">void )
public void SplDoublyLinkedList::next ( <span class="methodparam">void )
public bool
SplDoublyLinkedList::offsetExists (
mixed
$index )
public mixed
SplDoublyLinkedList::offsetGet ( <span
class="methodparam">mixed $index )
public void
SplDoublyLinkedList::offsetSet ( <span
class="methodparam">mixed $index ,
mixed
$newval )
public void
SplDoublyLinkedList::offsetUnset ( <span
class="methodparam">mixed $index )
public mixed SplDoublyLinkedList::pop ( <span class="methodparam">void )
public void SplDoublyLinkedList::prev ( <span class="methodparam">void )
public void
SplDoublyLinkedList::push ( <span
class="methodparam">mixed $value )
public void SplDoublyLinkedList::rewind ( <span class="methodparam">void )
public string SplDoublyLinkedList::serialize ( <span class="methodparam">void )
public void
SplDoublyLinkedList::setIteratorMode (
int $mode )
public mixed SplDoublyLinkedList::shift ( <span class="methodparam">void )
public mixed SplDoublyLinkedList::top ( <span class="methodparam">void )
public void
SplDoublyLinkedList::unserialize ( <span
class="methodparam">string
$serialized )
public void
SplDoublyLinkedList::unshift ( <span
class="methodparam">mixed $value )
public bool SplDoublyLinkedList::valid ( <span class="methodparam">void )
}
SplStack::__construct
Constructs a new stack implemented using a doubly linked list
说明
SplStack::__construct ( <span class="methodparam">void )
This constructs a new empty stack.
Note:
This method automatically sets the iterator mode to SplDoublyLinkedList::IT_MODE_LIFO.
参数
此函数没有参数。
返回值
没有返回值。
范例
示例 #1 SplStack::__construct example
<?php
$q = new SplStack();
$q[] = 1;
$q[] = 2;
$q[] = 3;
foreach ($q as $elem) {
echo $elem."\n";
}
?>
以上例程会输出:
3
2
1
SplStack::setIteratorMode
Sets the mode of iteration
说明
void <span
class="methodname">SplStack::setIteratorMode ( <span
class="methodparam">int $mode )
参数
mode
There is only one iteration parameter you can modify.
- The behavior of the iterator (either one or
the other):
- SplDoublyLinkedList::IT_MODE_DELETE (Elements are deleted by the iterator)
- SplDoublyLinkedList::IT_MODE_KEEP (Elements are traversed by the iterator)
The default mode is 0x2 : SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_KEEP
Warning The direction of iteration can no longer be changed for SplStacks. Trying to do so will result in a <span class="classname">RuntimeException being thrown.
返回值
没有返回值。
简介
SplQueue 类通过使用一个双向链表来提供队列的主要功能。
类摘要
SplQueue
class SplQueue extends SplDoublyLinkedList implements <span class="interfacename">Iterator <span class="oointerface">, ArrayAccess , <span class="interfacename">Countable {
/* 方法 */
__construct ( <span class="methodparam">void )
mixed dequeue ( void )
void enqueue (
mixed
$value )
void <span
class="methodname">setIteratorMode ( <span
class="methodparam">int $mode )
/* 继承的方法 */
public void
SplDoublyLinkedList::add ( <span
class="methodparam">mixed $index ,
mixed
$newval )
public mixed SplDoublyLinkedList::bottom ( <span class="methodparam">void )
public int <span class="methodname">SplDoublyLinkedList::count ( <span class="methodparam">void )
public mixed SplDoublyLinkedList::current ( <span class="methodparam">void )
public int <span class="methodname">SplDoublyLinkedList::getIteratorMode ( <span class="methodparam">void )
public bool SplDoublyLinkedList::isEmpty ( <span class="methodparam">void )
public mixed SplDoublyLinkedList::key ( <span class="methodparam">void )
public void SplDoublyLinkedList::next ( <span class="methodparam">void )
public bool
SplDoublyLinkedList::offsetExists (
mixed
$index )
public mixed
SplDoublyLinkedList::offsetGet ( <span
class="methodparam">mixed $index )
public void
SplDoublyLinkedList::offsetSet ( <span
class="methodparam">mixed $index ,
mixed
$newval )
public void
SplDoublyLinkedList::offsetUnset ( <span
class="methodparam">mixed $index )
public mixed SplDoublyLinkedList::pop ( <span class="methodparam">void )
public void SplDoublyLinkedList::prev ( <span class="methodparam">void )
public void
SplDoublyLinkedList::push ( <span
class="methodparam">mixed $value )
public void SplDoublyLinkedList::rewind ( <span class="methodparam">void )
public string SplDoublyLinkedList::serialize ( <span class="methodparam">void )
public void
SplDoublyLinkedList::setIteratorMode (
int $mode )
public mixed SplDoublyLinkedList::shift ( <span class="methodparam">void )
public mixed SplDoublyLinkedList::top ( <span class="methodparam">void )
public void
SplDoublyLinkedList::unserialize ( <span
class="methodparam">string
$serialized )
public void
SplDoublyLinkedList::unshift ( <span
class="methodparam">mixed $value )
public bool SplDoublyLinkedList::valid ( <span class="methodparam">void )
}
SplQueue::__construct
Constructs a new queue implemented using a doubly linked list
说明
SplQueue::__construct ( <span class="methodparam">void )
This constructs a new empty queue.
Note:
This method automatically sets the iterator mode to SplDoublyLinkedList::IT_MODE_FIFO.
参数
此函数没有参数。
返回值
没有返回值。
范例
示例 #1 SplQueue::__construct example
<?php
$q = new SplQueue();
$q[] = 1;
$q[] = 2;
$q[] = 3;
foreach ($q as $elem) {
echo $elem."\n";
}
?>
以上例程会输出:
1
2
3
示例 #2 Efficiently handling tasks with <span class="classname">SplQueue
<?php
$q = new SplQueue();
$q->setIteratorMode(SplQueue::IT_MODE_DELETE);
// ... enqueue some tasks on the queue ...
// process them
foreach ($q as $task) {
// ... process $task ...
// add new tasks on the queue
$q[] = $newTask;
// ...
}
?>
SplQueue::dequeue
Dequeues a node from the queue
说明
mixed <span class="methodname">SplQueue::dequeue ( <span class="methodparam">void )
Dequeues value from the top of the queue.
Note:
SplQueue::dequeue is an alias of <span class="methodname">SplDoublyLinkedList::shift.
参数
此函数没有参数。
返回值
The value of the dequeued node.
SplQueue::enqueue
Adds an element to the queue
说明
void <span
class="methodname">SplQueue::enqueue ( <span
class="methodparam">mixed $value )
Enqueues value at the end of the queue.
Note:
SplQueue::enqueue is an alias of <span class="methodname">SplDoublyLinkedList::push.
参数
value
The value to enqueue.
返回值
没有返回值。
SplQueue::setIteratorMode
Sets the mode of iteration
说明
void <span
class="methodname">SplQueue::setIteratorMode ( <span
class="methodparam">int $mode )
参数
mode
There is only one iteration parameter you can modify.
- The behavior of the iterator (either one or
the other):
SplDoublyLinkedList::IT_MODE_DELETE(Elements are deleted by the iterator)SplDoublyLinkedList::IT_MODE_KEEP(Elements are traversed by the iterator)
The default mode is: SplDoublyLinkedList::IT_MODE_FIFO |
SplDoublyLinkedList::IT_MODE_KEEP
Warning
The direction of iteration can not be changed for SplQueues, it is
always SplDoublyLinkedList::IT_MODE_FIFO.
返回值
没有返回值。
错误/异常
Throws a RuntimeException on trying to
change the direction of iteration by using
SplDoublyLinkedList::IT_MODE_LIFO.
简介
The SplHeap class provides the main functionalities of a Heap.
类摘要
SplHeap
abstract class SplHeap implements <span class="interfacename">Iterator <span class="oointerface">, Countable {
/* 方法 */
public <span class="methodname">__construct ( <span class="methodparam">void )
abstract <span
class="modifier">protected int <span
class="methodname">compare ( <span
class="type">mixed $value1 , <span
class="methodparam">mixed $value2 )
public int <span class="methodname">count ( void )
public mixed current ( <span class="methodparam">void )
public mixed extract ( <span class="methodparam">void )
public void
insert ( <span
class="type">mixed $value )
public bool isCorrupted ( <span class="methodparam">void )
public bool isEmpty ( <span class="methodparam">void )
public mixed key ( <span class="methodparam">void )
public void next ( <span class="methodparam">void )
public void recoverFromCorruption ( <span class="methodparam">void )
public void rewind ( <span class="methodparam">void )
public mixed top ( <span class="methodparam">void )
public bool valid ( <span class="methodparam">void )
}
SplHeap::compare
Compare elements in order to place them correctly in the heap while sifting up
说明
abstract <span
class="modifier">protected int <span
class="methodname">SplHeap::compare ( <span
class="methodparam">mixed $value1 ,
mixed
$value2 )
Compare value1 with value2.
Warning
Throwing exceptions in SplHeap::compare can corrupt the Heap and place it in a blocked state. You can unblock it by calling <span class="methodname">SplHeap::recoverFromCorruption. However, some elements might not be placed correctly and it may hence break the heap-property.
参数
value1
The value of the first node being compared.
value2
The value of the second node being compared.
返回值
Result of the comparison, positive integer if value1 is greater than
value2, 0 if they are equal, negative integer otherwise.
Note:
Having multiple elements with the same value in a Heap is not recommended. They will end up in an arbitrary relative position.
SplHeap::__construct
Constructs a new empty heap
说明
public <span class="methodname">SplHeap::__construct ( <span class="methodparam">void )
This constructs a new empty heap.
参数
此函数没有参数。
返回值
没有返回值。
SplHeap::count
Counts the number of elements in the heap
说明
public int <span class="methodname">SplHeap::count ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
Returns the number of elements in the heap.
SplHeap::current
Return current node pointed by the iterator
说明
public mixed SplHeap::current ( <span class="methodparam">void )
Get the current datastructure node.
参数
此函数没有参数。
返回值
The current node value.
SplHeap::extract
Extracts a node from top of the heap and sift up
说明
public mixed SplHeap::extract ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
The value of the extracted node.
错误/异常
Throws RuntimeException when the data-structure is empty.
SplHeap::insert
Inserts an element in the heap by sifting it up
说明
public void
SplHeap::insert ( <span
class="methodparam">mixed $value )
Insert value in the heap.
参数
value
The value to insert.
返回值
没有返回值。
SplHeap::isCorrupted
Tells if the heap is in a corrupted state
说明
public bool SplHeap::isCorrupted ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
Returns true if the heap is corrupted, false otherwise.
SplHeap::isEmpty
Checks whether the heap is empty
说明
public bool SplHeap::isEmpty ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
Returns whether the heap is empty.
SplHeap::key
Return current node index
说明
public mixed SplHeap::key ( <span class="methodparam">void )
This function returns the current node index
参数
此函数没有参数。
返回值
The current node index.
SplHeap::next
Move to the next node
说明
public void SplHeap::next ( <span class="methodparam">void )
Move to the next node. This will delete the top node of the heap.
参数
此函数没有参数。
返回值
没有返回值。
SplHeap::recoverFromCorruption
Recover from the corrupted state and allow further actions on the heap
说明
public void SplHeap::recoverFromCorruption ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
没有返回值。
SplHeap::rewind
Rewind iterator back to the start (no-op)
说明
public void SplHeap::rewind ( <span class="methodparam">void )
This rewinds the iterator to the beginning. This is a no-op for heaps as the iterator is virtual and in fact never moves from the top of the heap.
参数
此函数没有参数。
返回值
没有返回值。
SplHeap::top
Peeks at the node from the top of the heap
说明
public mixed SplHeap::top ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
The value of the node on the top.
错误/异常
Throws RuntimeException when the data-structure is empty.
SplHeap::valid
Check whether the heap contains more nodes
说明
public bool SplHeap::valid ( <span class="methodparam">void )
Checks if the heap contains any more nodes.
参数
此函数没有参数。
返回值
Returns true if the heap contains any more nodes, false
otherwise.
简介
The SplMaxHeap class provides the main functionalities of a heap, keeping the maximum on the top.
类摘要
SplMaxHeap
class SplMaxHeap <span class="ooclass"> extends SplHeap implements <span class="interfacename">Iterator <span class="oointerface">, Countable {
/* 方法 */
protected int
compare ( <span
class="methodparam">mixed $value1 ,
mixed
$value2 )
/* 继承的方法 */
abstract <span
class="modifier">protected int <span
class="methodname">SplHeap::compare ( <span
class="methodparam">mixed $value1 ,
mixed
$value2 )
public int <span class="methodname">SplHeap::count ( <span class="methodparam">void )
public mixed SplHeap::current ( <span class="methodparam">void )
public mixed SplHeap::extract ( <span class="methodparam">void )
public void
SplHeap::insert ( <span
class="methodparam">mixed $value )
public bool SplHeap::isCorrupted ( <span class="methodparam">void )
public bool SplHeap::isEmpty ( <span class="methodparam">void )
public mixed SplHeap::key ( <span class="methodparam">void )
public void SplHeap::next ( <span class="methodparam">void )
public void SplHeap::recoverFromCorruption ( <span class="methodparam">void )
public void SplHeap::rewind ( <span class="methodparam">void )
public mixed SplHeap::top ( <span class="methodparam">void )
public bool SplHeap::valid ( <span class="methodparam">void )
}
SplMaxHeap::compare
Compare elements in order to place them correctly in the heap while sifting up
说明
protected int
SplMaxHeap::compare ( <span
class="methodparam">mixed $value1 ,
mixed
$value2 )
Compare value1 with value2.
参数
value1
The value of the first node being compared.
value2
The value of the second node being compared.
返回值
Result of the comparison, positive integer if value1 is greater than
value2, 0 if they are equal, negative integer otherwise.
Note:
Having multiple elements with the same value in a Heap is not recommended. They will end up in an arbitrary relative position.
简介
The SplMinHeap class provides the main functionalities of a heap, keeping the minimum on the top.
类摘要
SplMinHeap
class SplMinHeap <span class="ooclass"> extends SplHeap implements <span class="interfacename">Iterator <span class="oointerface">, Countable {
/* 方法 */
protected int
compare ( <span
class="methodparam">mixed $value1 ,
mixed
$value2 )
/* 继承的方法 */
abstract <span
class="modifier">protected int <span
class="methodname">SplHeap::compare ( <span
class="methodparam">mixed $value1 ,
mixed
$value2 )
public int <span class="methodname">SplHeap::count ( <span class="methodparam">void )
public mixed SplHeap::current ( <span class="methodparam">void )
public mixed SplHeap::extract ( <span class="methodparam">void )
public void
SplHeap::insert ( <span
class="methodparam">mixed $value )
public bool SplHeap::isCorrupted ( <span class="methodparam">void )
public bool SplHeap::isEmpty ( <span class="methodparam">void )
public mixed SplHeap::key ( <span class="methodparam">void )
public void SplHeap::next ( <span class="methodparam">void )
public void SplHeap::recoverFromCorruption ( <span class="methodparam">void )
public void SplHeap::rewind ( <span class="methodparam">void )
public mixed SplHeap::top ( <span class="methodparam">void )
public bool SplHeap::valid ( <span class="methodparam">void )
}
SplMinHeap::compare
Compare elements in order to place them correctly in the heap while sifting up
说明
protected int
SplMinHeap::compare ( <span
class="methodparam">mixed $value1 ,
mixed
$value2 )
Compare value1 with value2.
参数
value1
The value of the first node being compared.
value2
The value of the second node being compared.
返回值
Result of the comparison, positive integer if value1 is lower than
value2, 0 if they are equal, negative integer otherwise.
Note:
Having multiple elements with the same value in a Heap is not recommended. They will end up in an arbitrary relative position.
简介
The SplPriorityQueue class provides the main functionalities of a prioritized queue, implemented using a max heap.
Note: The order of elements with identical priority is undefined. It may differ from the order in which they have been inserted.
类摘要
SplPriorityQueue
class SplPriorityQueue <span class="oointerface">implements <span class="interfacename">Iterator <span class="oointerface">, Countable {
/* 方法 */
public <span class="methodname">__construct ( <span class="methodparam">void )
public int <span
class="methodname">compare ( <span
class="type">mixed $priority1 , <span
class="methodparam">mixed $priority2
)
public int <span class="methodname">count ( void )
public mixed current ( <span class="methodparam">void )
public mixed extract ( <span class="methodparam">void )
public int <span class="methodname">getExtractFlags ( <span class="methodparam">void )
public bool
insert ( <span
class="type">mixed $value , <span
class="methodparam">mixed $priority )
public bool isCorrupted ( <span class="methodparam">void )
public bool isEmpty ( <span class="methodparam">void )
public mixed key ( <span class="methodparam">void )
public void next ( <span class="methodparam">void )
public void recoverFromCorruption ( <span class="methodparam">void )
public void rewind ( <span class="methodparam">void )
public void
setExtractFlags ( <span
class="methodparam">int $flags )
public mixed top ( <span class="methodparam">void )
public bool valid ( <span class="methodparam">void )
}
SplPriorityQueue::compare
Compare priorities in order to place elements correctly in the heap while sifting up
说明
public int <span
class="methodname">SplPriorityQueue::compare ( <span
class="methodparam">mixed $priority1
, mixed
$priority2 )
Compare priority1 with priority2.
参数
priority1
The priority of the first node being compared.
priority2
The priority of the second node being compared.
返回值
Result of the comparison, positive integer if priority1 is greater
than priority2, 0 if they are equal, negative integer otherwise.
Note:
Multiple elements with the same priority will get dequeued in no particular order.
SplPriorityQueue::__construct
Constructs a new empty queue
说明
public <span class="methodname">SplPriorityQueue::__construct ( <span class="methodparam">void )
This constructs a new empty queue.
参数
此函数没有参数。
返回值
没有返回值。
SplPriorityQueue::count
Counts the number of elements in the queue
说明
public int <span class="methodname">SplPriorityQueue::count ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
Returns the number of elements in the queue.
SplPriorityQueue::current
Return current node pointed by the iterator
说明
public mixed SplPriorityQueue::current ( <span class="methodparam">void )
Get the current datastructure node.
参数
此函数没有参数。
返回值
The value or priority (or both) of the current node, depending on the extract flag.
SplPriorityQueue::extract
Extracts a node from top of the heap and sift up
说明
public mixed SplPriorityQueue::extract ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
The value or priority (or both) of the extracted node, depending on the extract flag.
SplPriorityQueue::getExtractFlags
Get the flags of extraction
说明
public int <span class="methodname">SplPriorityQueue::getExtractFlags ( <span class="methodparam">void )
Warning
本函数还未编写文档,仅有参数列表。
参数
此函数没有参数。
返回值
SplPriorityQueue::insert
Inserts an element in the queue by sifting it up
说明
public bool
SplPriorityQueue::insert ( <span
class="methodparam">mixed $value ,
mixed
$priority )
Insert value with the priority priority in the queue.
参数
value
The value to insert.
priority
The associated priority.
返回值
Returns true.
SplPriorityQueue::isCorrupted
Tells if the priority queue is in a corrupted state
说明
public bool SplPriorityQueue::isCorrupted ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
Returns true if the priority queue is corrupted, false
otherwise.
SplPriorityQueue::isEmpty
Checks whether the queue is empty
说明
public bool SplPriorityQueue::isEmpty ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
Returns whether the queue is empty.
SplPriorityQueue::key
Return current node index
说明
public mixed SplPriorityQueue::key ( <span class="methodparam">void )
This function returns the current node index
参数
此函数没有参数。
返回值
The current node index.
SplPriorityQueue::next
Move to the next node
说明
public void SplPriorityQueue::next ( <span class="methodparam">void )
Extracts the top node from the queue.
参数
此函数没有参数。
返回值
没有返回值。
SplPriorityQueue::recoverFromCorruption
Recover from the corrupted state and allow further actions on the queue
说明
public void SplPriorityQueue::recoverFromCorruption ( void )
参数
此函数没有参数。
返回值
没有返回值。
SplPriorityQueue::rewind
Rewind iterator back to the start (no-op)
说明
public void SplPriorityQueue::rewind ( <span class="methodparam">void )
This rewinds the iterator to the beginning. This is a no-op for heaps as the iterator is virtual and in fact never moves from the top of the heap.
参数
此函数没有参数。
返回值
没有返回值。
SplPriorityQueue::setExtractFlags
Sets the mode of extraction
说明
public void
SplPriorityQueue::setExtractFlags (
int $flags
)
参数
flags
Defines what is extracted by <span
class="methodname">SplPriorityQueue::current, <span
class="methodname">SplPriorityQueue::top and <span
class="methodname">SplPriorityQueue::extract.
SplPriorityQueue::EXTR_DATA(0x00000001): Extract the dataSplPriorityQueue::EXTR_PRIORITY(0x00000002): Extract the prioritySplPriorityQueue::EXTR_BOTH(0x00000003): Extract an array containing both
The default mode is SplPriorityQueue::EXTR_DATA.
返回值
没有返回值。
SplPriorityQueue::top
Peeks at the node from the top of the queue
说明
public mixed SplPriorityQueue::top ( <span class="methodparam">void )
参数
此函数没有参数。
返回值
The value or priority (or both) of the top node, depending on the extract flag.
SplPriorityQueue::valid
Check whether the queue contains more nodes
说明
public bool SplPriorityQueue::valid ( <span class="methodparam">void )
Checks if the queue contains any more nodes.
参数
此函数没有参数。
返回值
Returns true if the queue contains any more nodes, false
otherwise.
简介
The SplFixedArray class provides the main functionalities of array. The main differences between a SplFixedArray and a normal PHP array is that the SplFixedArray is of fixed length and allows only integers within the range as indexes. The advantage is that it uses less memory than a standard array.
类摘要
SplFixedArray
class SplFixedArray <span class="oointerface">implements <span class="interfacename">Iterator <span class="oointerface">, ArrayAccess , <span class="interfacename">Countable {
/* 方法 */
public <span
class="methodname">__construct ([ <span
class="methodparam">int $size<span
class="initializer"> = 0 ] )
public int <span class="methodname">count ( void )
public mixed current ( <span class="methodparam">void )
public <span
class="modifier">static SplFixedArray
fromArray ( <span
class="methodparam">array $array [,
bool
$save_indexes = true ]
)
public int <span class="methodname">getSize ( <span class="methodparam">void )
public int <span class="methodname">key ( void )
public void next ( <span class="methodparam">void )
public bool
offsetExists ( <span
class="methodparam">int $index )
public mixed
offsetGet ( <span
class="methodparam">int $index )
public void
offsetSet ( <span
class="methodparam">int $index ,
mixed
$newval )
public void
offsetUnset ( <span
class="methodparam">int $index )
public void rewind ( <span class="methodparam">void )
public bool
setSize ( <span
class="methodparam">int $size )
public array toArray ( <span class="methodparam">void )
public bool valid ( <span class="methodparam">void )
public void __wakeup ( <span class="methodparam">void )
}
范例
示例 #1 SplFixedArray usage example
<?php
// Initialize the array with a fixed length
$array = new SplFixedArray(5);
$array[1] = 2;
$array[4] = "foo";
var_dump($array[0]); // NULL
var_dump($array[1]); // int(2)
var_dump($array["4"]); // string(3) "foo"
// Increase the size of the array to 10
$array->setSize(10);
$array[9] = "asdf";
// Shrink the array to a size of 2
$array->setSize(2);
// The following lines throw a RuntimeException: Index invalid or out of range
try {
var_dump($array["non-numeric"]);
} catch(RuntimeException $re) {
echo "RuntimeException: ".$re->getMessage()."\n";
}
try {
var_dump($array[-1]);
} catch(RuntimeException $re) {
echo "RuntimeException: ".$re->getMessage()."\n";
}
try {
var_dump($array[5]);
} catch(RuntimeException $re) {
echo "RuntimeException: ".$re->getMessage()."\n";
}
?>
以上例程会输出:
NULL
int(2)
string(3) "foo"
RuntimeException: Index invalid or out of range
RuntimeException: Index invalid or out of range
RuntimeException: Index invalid or out of range
SplFixedArray::__construct
Constructs a new fixed array
说明
public <span
class="methodname">SplFixedArray::__construct ([ <span
class="methodparam">int $size<span
class="initializer"> = 0 ] )
Initializes a fixed array with a number of null values equal to
size.
参数
size
The size of the fixed array. This expects a number between 0 and
PHP_INT_MAX.
返回值
没有返回值。
错误/异常
Throws InvalidArgumentException when
size is a negative number.
Raises E_WARNING when size cannot be parsed as a number.
范例
示例 #1 SplFixedArray::__construct example
<?php
$array = new SplFixedArray(5);
$array[1] = 2;
$array[4] = "foo";
foreach($array as $v) {
var_dump($v);
}
?>
以上例程会输出:
NULL
int(2)
NULL
NULL
string(3) "foo"
SplFixedArray::count
Returns the size of the array
说明
public int <span class="methodname">SplFixedArray::count ( <span class="methodparam">void )
Returns the size of the array.
参数
此函数没有参数。
返回值
Returns the size of the array.
范例
示例 #1 SplFixedArray::count example
<?php
$array = new SplFixedArray(5);
echo $array->count() . "\n";
echo count($array) . "\n";
?>
以上例程会输出:
5
5
注释
Note:
This method is functionally equivalent to <span class="methodname">SplFixedArray::getSize.
Note:
The count of elements is always equal to the set size because all values are initially initialized with
null.
参见
- SplFixedArray::getSize
SplFixedArray::current
Return current array entry
说明
public mixed SplFixedArray::current ( <span class="methodparam">void )
Get the current array element.
参数
此函数没有参数。
返回值
The current element value.
错误/异常
Throws RuntimeException when the internal array pointer points to an invalid index or is out of bounds.
SplFixedArray::fromArray
Import a PHP array in a SplFixedArray instance
说明
public <span
class="modifier">static SplFixedArray
SplFixedArray::fromArray ( <span
class="methodparam">array $array [,
bool
$save_indexes = true ]
)
Import the PHP array array in a new <span
class="classname">SplFixedArray instance
参数
array
The array to import.
save_indexes
Try to save the numeric indexes used in the original array.
返回值
Returns an instance of SplFixedArray containing the array content.
范例
示例 #1 SplFixedArray::fromArray example
<?php
$fa = SplFixedArray::fromArray(array(1 => 1, 0 => 2, 3 => 3));
var_dump($fa);
$fa = SplFixedArray::fromArray(array(1 => 1, 0 => 2, 3 => 3), false);
var_dump($fa);
?>
以上例程会输出:
object(SplFixedArray)#1 (4) {
[0]=>
int(2)
[1]=>
int(1)
[2]=>
NULL
[3]=>
int(3)
}
object(SplFixedArray)#2 (3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
SplFixedArray::getSize
Gets the size of the array
说明
public int <span class="methodname">SplFixedArray::getSize ( <span class="methodparam">void )
Gets the size of the array.
参数
此函数没有参数。
返回值
Returns the size of the array, as an int.
范例
示例 #1 SplFixedArray::getSize example
<?php
$array = new SplFixedArray(5);
echo $array->getSize()."\n";
$array->setSize(10);
echo $array->getSize()."\n";
?>
以上例程会输出:
5
10
注释
Note:
This method is functionally equivalent to <span class="methodname">SplFixedArray::count
参见
- SplFixedArray::count
SplFixedArray::key
Return current array index
说明
public int <span class="methodname">SplFixedArray::key ( <span class="methodparam">void )
Returns the current array index.
参数
此函数没有参数。
返回值
The current array index.
SplFixedArray::next
Move to next entry
说明
public void SplFixedArray::next ( <span class="methodparam">void )
Move the iterator to the next array entry.
参数
此函数没有参数。
返回值
没有返回值。
SplFixedArray::offsetExists
Returns whether the requested index exists
说明
public bool
SplFixedArray::offsetExists ( <span
class="methodparam">int $index )
Checks whether the requested index index exists.
参数
index
The index being checked.
返回值
true if the requested index exists, otherwise false
SplFixedArray::offsetGet
Returns the value at the specified index
说明
public mixed
SplFixedArray::offsetGet ( <span
class="methodparam">int $index )
Returns the value at the index index.
参数
index
The index with the value.
返回值
The value at the specified index.
错误/异常
Throws RuntimeException when index is
outside the defined size of the array or when index cannot be parsed
as an integer.
SplFixedArray::offsetSet
Sets a new value at a specified index
说明
public void
SplFixedArray::offsetSet ( <span
class="methodparam">int $index ,
mixed
$newval )
Sets the value at the specified index to newval.
参数
index
The index being set.
newval
The new value for the index.
返回值
没有返回值。
错误/异常
Throws RuntimeException when index is
outside the defined size of the array or when index cannot be parsed
as an integer.
SplFixedArray::offsetUnset
Unsets the value at the specified $index
说明
public void
SplFixedArray::offsetUnset ( <span
class="methodparam">int $index )
Unsets the value at the specified index.
参数
index
The index being unset.
返回值
没有返回值。
错误/异常
Throws RuntimeException when index is
outside the defined size of the array or when index cannot be parsed
as an integer.
SplFixedArray::rewind
Rewind iterator back to the start
说明
public void SplFixedArray::rewind ( <span class="methodparam">void )
Rewinds the iterator to the beginning.
参数
此函数没有参数。
返回值
没有返回值。
SplFixedArray::setSize
Change the size of an array
说明
public bool
SplFixedArray::setSize ( <span
class="methodparam">int $size )
Change the size of an array to the new size of size. If size is less
than the current array size, any values after the new size will be
discarded. If size is greater than the current array size, the array
will be padded with null values.
参数
size
The new array size. This should be a value between 0 and
PHP_INT_MAX.
返回值
成功时返回 true, 或者在失败时返回 false。
错误/异常
Throws InvalidArgumentException when
size is less than zero.
Raises E_WARNING when size cannot be used as a number.
范例
示例 #1 SplFixedArray::setSize example
<?php
$array = new SplFixedArray(5);
echo $array->getSize()."\n";
$array->setSize(10);
echo $array->getSize()."\n";
?>
以上例程会输出:
5
10
SplFixedArray::toArray
Returns a PHP array from the fixed array
说明
public array SplFixedArray::toArray ( <span class="methodparam">void )
Returns a PHP array from the fixed array.
参数
此函数没有参数。
返回值
Returns a PHP array, similar to the fixed array.
范例
示例 #1 SplFixedArray::toArray example
<?php
$fa = new SplFixedArray(3);
$fa[0] = 0;
$fa[2] = 2;
var_dump($fa->toArray());
?>
以上例程会输出:
array(3) {
[0]=>
int(0)
[1]=>
NULL
[2]=>
int(2)
}
SplFixedArray::valid
Check whether the array contains more elements
说明
public bool SplFixedArray::valid ( <span class="methodparam">void )
Checks if the array contains any more elements.
参数
此函数没有参数。
返回值
Returns true if the array contains any more elements, false
otherwise.
SplFixedArray::__wakeup
Reinitialises the array after being unserialised
说明
public void SplFixedArray::__wakeup ( <span class="methodparam">void )
Reinitialises the array after being unserialised.
参数
此函数没有参数。
返回值
没有返回值。
简介
The SplObjectStorage class provides a map from objects to data or, by ignoring data, an object set. This dual purpose can be useful in many cases involving the need to uniquely identify objects.
类摘要
SplObjectStorage
class SplObjectStorage <span class="oointerface">implements <span class="interfacename">Countable <span class="oointerface">, Iterator , <span class="interfacename">Serializable <span class="oointerface">, ArrayAccess {
/* 方法 */
public void
addAll ( <span
class="type">SplObjectStorage $storage )
public void
attach ( <span
class="type">object $object [, <span
class="methodparam">mixed $data<span
class="initializer"> = null ] )
public bool
contains ( <span
class="methodparam">object $object )
public int <span class="methodname">count ( void )
public object current ( <span class="methodparam">void )
public void
detach ( <span
class="type">object $object )
public string
getHash ( <span
class="methodparam">object $object )
public mixed getInfo ( <span class="methodparam">void )
public int <span class="methodname">key ( void )
public void next ( <span class="methodparam">void )
public bool
offsetExists ( <span
class="methodparam">object $object )
public mixed
offsetGet ( <span
class="methodparam">object $object )
public void
offsetSet ( <span
class="methodparam">object $object
[, mixed
$data = null ] )
public void
offsetUnset ( <span
class="methodparam">object $object )
public void
removeAll ( <span
class="methodparam">SplObjectStorage
$storage )
public void
removeAllExcept ( <span
class="methodparam">SplObjectStorage
$storage )
public void rewind ( <span class="methodparam">void )
public string serialize ( <span class="methodparam">void )
public void
setInfo ( <span
class="methodparam">mixed $data )
public void
unserialize ( <span
class="methodparam">string
$serialized )
public bool valid ( <span class="methodparam">void )
}
范例
示例 #1 SplObjectStorage as a set
<?php
// As an object set
$s = new SplObjectStorage();
$o1 = new StdClass;
$o2 = new StdClass;
$o3 = new StdClass;
$s->attach($o1);
$s->attach($o2);
var_dump($s->contains($o1));
var_dump($s->contains($o2));
var_dump($s->contains($o3));
$s->detach($o2);
var_dump($s->contains($o1));
var_dump($s->contains($o2));
var_dump($s->contains($o3));
?>
以上例程会输出:
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
bool(false)
示例 #2 SplObjectStorage as a map
<?php
// As a map from objects to data
$s = new SplObjectStorage();
$o1 = new StdClass;
$o2 = new StdClass;
$o3 = new StdClass;
$s[$o1] = "data for object 1";
$s[$o2] = array(1,2,3);
if (isset($s[$o2])) {
var_dump($s[$o2]);
}
?>
以上例程会输出:
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
SplObjectStorage::addAll
Adds all objects from another storage
说明
public void
SplObjectStorage::addAll ( <span
class="methodparam">SplObjectStorage
$storage )
Adds all objects-data pairs from a different storage in the current storage.
参数
storage
The storage you want to import.
返回值
没有返回值。
范例
示例 #1 SplObjectStorage::addAll example
<?php
$o = new StdClass;
$a = new SplObjectStorage();
$a[$o] = "hello";
$b = new SplObjectStorage();
$b->addAll($a);
echo $b[$o]."\n";
?>
以上例程的输出类似于:
hello
参见
- SplObjectStorage::removeAll
SplObjectStorage::attach
Adds an object in the storage
说明
public void
SplObjectStorage::attach ( <span
class="methodparam">object $object
[, mixed
$data = null ] )
Adds an object inside the storage, and optionally associate it to some data.
参数
object
The object to add.
data
The data to associate with the object.
返回值
没有返回值。
范例
示例 #1 SplObjectStorage::attach example
<?php
$o1 = new StdClass;
$o2 = new StdClass;
$s = new SplObjectStorage();
$s->attach($o1); // similar to $s[$o1] = NULL;
$s->attach($o2, "hello"); // similar to $s[$o2] = "hello";
var_dump($s[$o1]);
var_dump($s[$o2]);
?>
以上例程的输出类似于:
NULL
string(5) "hello"
参见
- SplObjectStorage::detach
- SplObjectStorage::offsetSet
SplObjectStorage::contains
Checks if the storage contains a specific object
说明
public bool
SplObjectStorage::contains ( <span
class="methodparam">object $object )
Checks if the storage contains the object provided.
参数
object
The object to look for.
返回值
Returns true if the object is in the
storage, false otherwise.
范例
示例 #1 SplObjectStorage::contains example
<?php
$o1 = new StdClass;
$o2 = new StdClass;
$s = new SplObjectStorage();
$s[$o1] = "hello";
var_dump($s->contains($o1));
var_dump($s->contains($o2));
?>
以上例程的输出类似于:
bool(true)
bool(false)
参见
- SplObjectStorage::offsetExists
SplObjectStorage::count
Returns the number of objects in the storage
说明
public int <span class="methodname">SplObjectStorage::count ( <span class="methodparam">void )
Counts the number of objects in the storage.
参数
此函数没有参数。
返回值
The number of objects in the storage.
范例
示例 #1 SplObjectStorage::count example
<?php
$s = new SplObjectStorage();
$o1 = new StdClass;
$o2 = new StdClass;
$s->attach($o1);
$s->attach($o2);
$s->attach($o1);
var_dump($s->count());
var_dump(count($s));
?>
以上例程的输出类似于:
int(2)
int(2)
参见
- SplObjectStorage::attach
- SplObjectStorage::detach
SplObjectStorage::current
Returns the current storage entry
说明
public object SplObjectStorage::current ( <span class="methodparam">void )
Returns the current storage entry.
参数
此函数没有参数。
返回值
The object at the current iterator position.
范例
示例 #1 SplObjectStorage::current example
<?php
$s = new SplObjectStorage();
$o1 = new StdClass;
$o2 = new StdClass;
$s->attach($o1, "d1");
$s->attach($o2, "d2");
$s->rewind();
while($s->valid()) {
$index = $s->key();
$object = $s->current(); // similar to current($s)
$data = $s->getInfo();
var_dump($object);
var_dump($data);
$s->next();
}
?>
以上例程的输出类似于:
object(stdClass)#2 (0) {
}
string(2) "d1"
object(stdClass)#3 (0) {
}
string(2) "d2"
参见
- SplObjectStorage::rewind
- SplObjectStorage::key
- SplObjectStorage::next
- SplObjectStorage::valid
- SplObjectStorage::getInfo
SplObjectStorage::detach
Removes an object from the storage
说明
public void
SplObjectStorage::detach ( <span
class="methodparam">object $object )
Removes the object from the storage.
参数
object
The object to remove.
返回值
没有返回值。
范例
示例 #1 SplObjectStorage::detach example
<?php
$o = new StdClass;
$s = new SplObjectStorage();
$s->attach($o);
var_dump(count($s));
$s->detach($o);
var_dump(count($s));
?>
以上例程的输出类似于:
int(1)
int(0)
参见
- SplObjectStorage::attach
- SplObjectStorage::removeAll
SplObjectStorage::getHash
Calculate a unique identifier for the contained objects
说明
public string
SplObjectStorage::getHash ( <span
class="methodparam">object $object )
This method calculates an identifier for the objects added to an <span class="classname">SplObjectStorage object.
The implementation in SplObjectStorage returns the same value as <span class="function">spl_object_hash.
The storage object will never contain more than one object with the same identifier. As such, it can be used to implement a set (a collection of unique values) where the quality of an object being unique is determined by the value returned by this function being unique.
参数
object
The object whose identifier is to be calculated.
返回值
A string with the calculated identifier.
错误/异常
A RuntimeException is thrown when the returned value is not a string.
范例
示例 #1 SplObjectStorage::getHash example
<?php
class OneSpecimenPerClassStorage extends SplObjectStorage {
public function getHash($o) {
return get_class($o);
}
}
class A {}
$s = new OneSpecimenPerClassStorage;
$o1 = new stdClass;
$o2 = new stdClass;
$o3 = new A;
$s[$o1] = 1;
//$o2 is considered equal to $o1 so the value is replaced
$s[$o2] = 2;
$s[$o3] = 3;
//these are considered equal to the objects before
//so they can be used to access the values stored under them
$p1 = new stdClass;
$p2 = new A;
echo $s[$p1], "\n";
echo $s[$p2], "\n";
?>
以上例程的输出类似于:
2
3
参见
- spl_object_hash
SplObjectStorage::getInfo
Returns the data associated with the current iterator entry
说明
public mixed SplObjectStorage::getInfo ( <span class="methodparam">void )
Returns the data, or info, associated with the object pointed by the current iterator position.
参数
此函数没有参数。
返回值
The data associated with the current iterator position.
范例
示例 #1 SplObjectStorage::getInfo example
<?php
$s = new SplObjectStorage();
$o1 = new StdClass;
$o2 = new StdClass;
$s->attach($o1, "d1");
$s->attach($o2, "d2");
$s->rewind();
while($s->valid()) {
$index = $s->key();
$object = $s->current(); // similar to current($s)
$data = $s->getInfo();
var_dump($object);
var_dump($data);
$s->next();
}
?>
以上例程的输出类似于:
object(stdClass)#2 (0) {
}
string(2) "d1"
object(stdClass)#3 (0) {
}
string(2) "d2"
参见
- SplObjectStorage::current
- SplObjectStorage::rewind
- SplObjectStorage::key
- SplObjectStorage::next
- SplObjectStorage::valid
- SplObjectStorage::setInfo
SplObjectStorage::key
Returns the index at which the iterator currently is
说明
public int <span class="methodname">SplObjectStorage::key ( <span class="methodparam">void )
Returns the index at which the iterator currently is.
参数
此函数没有参数。
返回值
The index corresponding to the position of the iterator.
范例
示例 #1 SplObjectStorage::key example
<?php
$s = new SplObjectStorage();
$o1 = new StdClass;
$o2 = new StdClass;
$s->attach($o1, "d1");
$s->attach($o2, "d2");
$s->rewind();
while($s->valid()) {
$index = $s->key();
$object = $s->current(); // similar to current($s)
var_dump($index);
var_dump($object);
$s->next();
}
?>
以上例程的输出类似于:
int(0)
object(stdClass)#2 (0) {
}
int(1)
object(stdClass)#3 (0) {
}
参见
- SplObjectStorage::rewind
- SplObjectStorage::current
- SplObjectStorage::next
- SplObjectStorage::valid
SplObjectStorage::next
Move to the next entry
说明
public void SplObjectStorage::next ( <span class="methodparam">void )
Moves the iterator to the next object in the storage.
参数
此函数没有参数。
返回值
没有返回值。
范例
示例 #1 SplObjectStorage::next example
<?php
$s = new SplObjectStorage();
$o1 = new StdClass;
$o2 = new StdClass;
$s->attach($o1, "d1");
$s->attach($o2, "d2");
$s->rewind();
while($s->valid()) {
$index = $s->key();
$object = $s->current(); // similar to current($s)
var_dump($index);
var_dump($object);
$s->next();
}
?>
以上例程的输出类似于:
int(0)
object(stdClass)#2 (0) {
}
int(1)
object(stdClass)#3 (0) {
}
参见
- SPLObjectStorage::rewind
SplObjectStorage::offsetExists
Checks whether an object exists in the storage
说明
public bool
SplObjectStorage::offsetExists ( <span
class="methodparam">object $object )
Checks whether an object exists in the storage.
Note:
SplObjectStorage::offsetExists is an alias of SplObjectStorage::contains.
参数
object
The object to look for.
返回值
Returns true if the object exists in the
storage, and false otherwise.
范例
示例 #1 SplObjectStorage::offsetExists example
<?php
$s = new SplObjectStorage;
$o1 = new StdClass;
$o2 = new StdClass;
$s->attach($o1);
var_dump($s->offsetExists($o1)); // Similar to isset($s[$o1])
var_dump($s->offsetExists($o2)); // Similar to isset($s[$o2])
?>
以上例程的输出类似于:
bool(true)
bool(false)
参见
- SplObjectStorage::offsetSet
- SplObjectStorage::offsetGet
- SplObjectStorage::offsetUnset
SplObjectStorage::offsetGet
Returns the data associated with an object
说明
public mixed
SplObjectStorage::offsetGet ( <span
class="methodparam">object $object )
Returns the data associated with an object in the storage.
参数
object
The object to look for.
返回值
The data previously associated with the object in the storage.
错误/异常
Throws UnexpectedValueException when
object could not be found.
范例
示例 #1 SplObjectStorage::offsetGet example
<?php
$s = new SplObjectStorage;
$o1 = new StdClass;
$o2 = new StdClass;
$s[$o1] = "hello";
$s->attach($o2);
var_dump($s->offsetGet($o1)); // Similar to $s[$o1]
var_dump($s->offsetGet($o2)); // Similar to $s[$o2]
?>
以上例程的输出类似于:
string(5) "hello"
NULL
参见
- SplObjectStorage::offsetSet
- SplObjectStorage::offsetExists
- SplObjectStorage::offsetUnset
SplObjectStorage::offsetSet
Associates data to an object in the storage
说明
public void
SplObjectStorage::offsetSet ( <span
class="methodparam">object $object
[, mixed
$data = null ] )
Associate data to an object in the storage.
Note:
SplObjectStorage::offsetSet is an alias of SplObjectStorage::attach.
参数
object
The object to associate data with.
data
The data to associate with the object.
返回值
没有返回值。
范例
示例 #1 SplObjectStorage::offsetSet example
<?php
$s = new SplObjectStorage;
$o1 = new StdClass;
$s->offsetSet($o1, "hello"); // Similar to $s[$o1] = "hello";
var_dump($s[$o1]);
?>
以上例程的输出类似于:
string(5) "hello"
参见
- SplObjectStorage::attach
- SplObjectStorage::offsetGet
- SplObjectStorage::offsetExists
- SplObjectStorage::offsetUnset
SplObjectStorage::offsetUnset
Removes an object from the storage
说明
public void
SplObjectStorage::offsetUnset ( <span
class="methodparam">object $object )
Removes an object from the storage.
Note:
SplObjectStorage::offsetUnset is an alias of SplObjectStorage::detach.
参数
object
The object to remove.
返回值
没有返回值。
范例
示例 #1 SplObjectStorage::offsetUnset example
<?php
$o = new StdClass;
$s = new SplObjectStorage();
$s->attach($o);
var_dump(count($s));
$s->offsetUnset($o); // Similar to unset($s[$o])
var_dump(count($s));
?>
以上例程的输出类似于:
int(1)
int(0)
参见
- SplObjectStorage::offsetGet
- SplObjectStorage::offsetSet
- SplObjectStorage::offsetExists
SplObjectStorage::removeAll
Removes objects contained in another storage from the current storage
说明
public void
SplObjectStorage::removeAll ( <span
class="methodparam">SplObjectStorage
$storage )
Removes objects contained in another storage from the current storage.
参数
storage
The storage containing the elements to remove.
返回值
没有返回值。
范例
示例 #1 SplObjectStorage::removeAll example
<?php
$o1 = new StdClass;
$o2 = new StdClass;
$a = new SplObjectStorage();
$a[$o1] = "foo";
$b = new SplObjectStorage();
$b[$o1] = "bar";
$b[$o2] = "gee";
var_dump(count($b));
$b->removeAll($a);
var_dump(count($b));
?>
以上例程的输出类似于:
int(2)
int(1)
参见
- SplObjectStorage::addAll
SplObjectStorage::removeAllExcept
Removes all objects except for those contained in another storage from the current storage
说明
public void
SplObjectStorage::removeAllExcept (
SplObjectStorage
$storage )
Removes all objects except for those contained in another storage from the current storage.
参数
storage
The storage containing the elements to retain in the current storage.
返回值
没有返回值。
范例
示例 #1 <span class="function">SplObjectStorage::removeAllExcept example
<?php
$a = (object) 'a';
$b = (object) 'b';
$c = (object) 'c';
$foo = new SplObjectStorage;
$foo->attach($a);
$foo->attach($b);
$bar = new SplObjectStorage;
$bar->attach($b);
$bar->attach($c);
$foo->removeAllExcept($bar);
var_dump($foo->contains($a));
var_dump($foo->contains($b));
?>
以上例程的输出类似于:
bool(false)
bool(true)
SplObjectStorage::rewind
Rewind the iterator to the first storage element
说明
public void SplObjectStorage::rewind ( <span class="methodparam">void )
Rewind the iterator to the first storage element.
参数
此函数没有参数。
返回值
没有返回值。
范例
示例 #1 SplObjectStorage::rewind example
<?php
$s = new SplObjectStorage();
$o1 = new StdClass;
$o2 = new StdClass;
$s->attach($o1, "d1");
$s->attach($o2, "d2");
$s->rewind();
while($s->valid()) {
$index = $s->key();
$object = $s->current(); // similar to current($s)
$data = $s->getInfo();
var_dump($object);
var_dump($data);
$s->next();
}
?>
以上例程的输出类似于:
int(1)
int(0)
参见
- SplObjectStorage::next
SplObjectStorage::serialize
Serializes the storage
说明
public string SplObjectStorage::serialize ( <span class="methodparam">void )
Returns a string representation of the storage.
参数
此函数没有参数。
返回值
A string representing the storage.
范例
示例 #1 SplObjectStorage::serialize example
<?php
$s = new SplObjectStorage;
$o = new StdClass;
$s[$o] = "data";
echo $s->serialize()."\n";
?>
以上例程的输出类似于:
x:i:1;O:8:"stdClass":0:{},s:4:"data";;m:a:0:{}
参见
- SplObjectStorage::unserialize
SplObjectStorage::setInfo
Sets the data associated with the current iterator entry
说明
public void
SplObjectStorage::setInfo ( <span
class="methodparam">mixed $data )
Associates data, or info, with the object currently pointed to by the iterator.
参数
data
The data to associate with the current iterator entry.
返回值
没有返回值。
范例
示例 #1 SplObjectStorage::setInfo example
<?php
$s = new SplObjectStorage();
$o1 = new StdClass;
$o2 = new StdClass;
$s->attach($o1, "d1");
$s->attach($o2, "d2");
$s->rewind();
while($s->valid()) {
$s->setInfo("new");
$s->next();
}
var_dump($s[$o1]);
var_dump($s[$o2]);
?>
以上例程的输出类似于:
string(3) "new"
string(3) "new"
参见
- SplObjectStorage::current
- SplObjectStorage::rewind
- SplObjectStorage::key
- SplObjectStorage::next
- SplObjectStorage::valid
- SplObjectStorage::getInfo
SplObjectStorage::unserialize
Unserializes a storage from its string representation
说明
public void
SplObjectStorage::unserialize ( <span
class="methodparam">string
$serialized )
Unserializes storage entries and attach them to the current storage.
参数
serialized
The serialized representation of a storage.
返回值
没有返回值。
范例
示例 #1 SplObjectStorage::unserialize example
<?php
$s1 = new SplObjectStorage;
$s2 = new SplObjectStorage;
$o = new StdClass;
$s1[$o] = "data";
$s2->unserialize($s1->serialize());
var_dump(count($s2));
?>
以上例程的输出类似于:
int(1)
参见
- SplObjectStorage::serialize
SplObjectStorage::valid
Returns if the current iterator entry is valid
说明
public bool SplObjectStorage::valid ( <span class="methodparam">void )
Returns if the current iterator entry is valid.
参数
此函数没有参数。
返回值
Returns true if the iterator entry is valid, false
otherwise.
范例
示例 #1 SplObjectStorage::valid example
<?php
$s = new SplObjectStorage();
$o1 = new StdClass;
$o2 = new StdClass;
$s->attach($o1, "d1");
$s->attach($o2, "d2");
$s->rewind();
while($s->valid()) {
echo $s->key()."\n";
$s->next();
}
?>
以上例程的输出类似于:
0
1
参见
- SplObjectStorage::current
- SplObjectStorage::getInfo