数据结构

目录

SPL 提供了一套标准的数据结构。它们按底层实现进行分组, 通常定义了它们的一般应用领域。

双向链表

双链表 (DLL) 是一个链接到两个方向的节点列表。当底层结构是 DLL 时, 迭代器的操作、对两端的访问、节点的添加或删除都具有 O (1) 的开销。因此, 它为栈和队列提供了一个合适的实现。

  • SplDoublyLinkedList
    • SplStack
    • SplQueue

堆是遵循堆属性的树状结构: 每个节点都大于或等于其子级, 使用对堆全局的已实现的比较方法进行比较。

  • SplHeap
    • SplMaxHeap
    • SplMinHeap
  • SplPriorityQueue

数组

数组是以连续方式存储数据的结构, 可通过索引进行访问。不要将它们与 php 数组混淆: php 数组实际上是按照有序的列表实现的。

  • SplFixedArray

映射

映射是一个数据拥有键值对。PHP 数组可以被看作是从整数/字符串到值的映射。SPL 提供了从对象到数据的映射。此映射也可用作对象集。

  • SplObjectStorage

简介

The SplDoublyLinkedList class provides the main functionalities of a doubly linked list.

类摘要

SplDoublyLinkedList

class SplDoublyLinkedList implements Iterator , ArrayAccess , Countable , Serializable {

/* 常量 */

const integer SplDoublyLinkedList::IT_MODE_LIFO = 2 ;

const integer SplDoublyLinkedList::IT_MODE_FIFO = 0 ;

const integer SplDoublyLinkedList::IT_MODE_DELETE = 1 ;

const integer SplDoublyLinkedList::IT_MODE_KEEP = 0 ;

/* 方法 */

public __construct ( void )

public void add ( mixed $index , mixed $newval )

public mixed bottom ( void )

public int count ( void )

public mixed current ( void )

public int getIteratorMode ( void )

public bool isEmpty ( void )

public mixed key ( void )

public void next ( void )

public bool offsetExists ( mixed $index )

public mixed offsetGet ( mixed $index )

public void offsetSet ( mixed $index , mixed $newval )

public void offsetUnset ( mixed $index )

public mixed pop ( void )

public void prev ( void )

public void push ( mixed $value )

public void rewind ( void )

public string serialize ( void )

public void setIteratorMode ( int $mode )

public mixed shift ( void )

public mixed top ( void )

public void unserialize ( string $serialized )

public void unshift ( mixed $value )

public bool valid ( 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 ( 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 ( void )

参数

此函数没有参数。

返回值

The value of the first node.

错误/异常

Throws RuntimeException when the data-structure is empty.

SplDoublyLinkedList::__construct

Constructs a new doubly linked list

说明

public SplDoublyLinkedList::__construct ( void )

This constructs a new empty doubly linked list.

参数

此函数没有参数。

返回值

没有返回值。

范例

示例 #1 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 SplDoublyLinkedList::count ( void )

参数

此函数没有参数。

返回值

Returns the number of elements in the doubly linked list.

SplDoublyLinkedList::current

Return current array entry

说明

public mixed SplDoublyLinkedList::current ( void )

Get the current doubly linked list node.

参数

此函数没有参数。

返回值

The current node value.

SplDoublyLinkedList::getIteratorMode

Returns the mode of iteration

说明

public int SplDoublyLinkedList::getIteratorMode ( 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 ( void )

参数

此函数没有参数。

返回值

Returns whether the doubly linked list is empty.

SplDoublyLinkedList::key

Return current node index

说明

public mixed SplDoublyLinkedList::key ( void )

This function returns the current node index

参数

此函数没有参数。

返回值

The current node index.

SplDoublyLinkedList::next

Move to next entry

说明

public void SplDoublyLinkedList::next ( 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 ( 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 ( 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 ( 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 ( 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 ( 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 ( 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 ( void )

This rewinds the iterator to the beginning.

参数

此函数没有参数。

返回值

没有返回值。

SplDoublyLinkedList::serialize

Serializes the storage

说明

public string SplDoublyLinkedList::serialize ( 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 ( 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 ( void )

参数

此函数没有参数。

返回值

The value of the last node.

错误/异常

Throws RuntimeException when the data-structure is empty.

SplDoublyLinkedList::unserialize

Unserializes the storage

说明

public void SplDoublyLinkedList::unserialize ( string $serialized )

Unserializes the storage, from SplDoublyLinkedList::serialize.

Warning

本函数还未编写文档,仅有参数列表。

参数

serialized
The serialized string.

返回值

没有返回值。

参见

  • SplDoublyLinkedList::serialize

SplDoublyLinkedList::unshift

Prepends the doubly linked list with an element

说明

public void SplDoublyLinkedList::unshift ( 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 ( 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 Iterator , ArrayAccess , Countable {

/* 方法 */

__construct ( void )

void setIteratorMode ( int $mode )

/* 继承的方法 */

public void SplDoublyLinkedList::add ( mixed $index , mixed $newval )

public mixed SplDoublyLinkedList::bottom ( void )

public int SplDoublyLinkedList::count ( void )

public mixed SplDoublyLinkedList::current ( void )

public int SplDoublyLinkedList::getIteratorMode ( void )

public bool SplDoublyLinkedList::isEmpty ( void )

public mixed SplDoublyLinkedList::key ( void )

public void SplDoublyLinkedList::next ( void )

public bool SplDoublyLinkedList::offsetExists ( mixed $index )

public mixed SplDoublyLinkedList::offsetGet ( mixed $index )

public void SplDoublyLinkedList::offsetSet ( mixed $index , mixed $newval )

public void SplDoublyLinkedList::offsetUnset ( mixed $index )

public mixed SplDoublyLinkedList::pop ( void )

public void SplDoublyLinkedList::prev ( void )

public void SplDoublyLinkedList::push ( mixed $value )

public void SplDoublyLinkedList::rewind ( void )

public string SplDoublyLinkedList::serialize ( void )

public void SplDoublyLinkedList::setIteratorMode ( int $mode )

public mixed SplDoublyLinkedList::shift ( void )

public mixed SplDoublyLinkedList::top ( void )

public void SplDoublyLinkedList::unserialize ( string $serialized )

public void SplDoublyLinkedList::unshift ( mixed $value )

public bool SplDoublyLinkedList::valid ( void )

}

SplStack::__construct

Constructs a new stack implemented using a doubly linked list

说明

SplStack::__construct ( 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 SplStack::setIteratorMode ( 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 RuntimeException being thrown.

返回值

没有返回值。

简介

SplQueue 类通过使用一个双向链表来提供队列的主要功能。

类摘要

SplQueue

class SplQueue extends SplDoublyLinkedList implements Iterator , ArrayAccess , Countable {

/* 方法 */

__construct ( void )

mixed dequeue ( void )

void enqueue ( mixed $value )

void setIteratorMode ( int $mode )

/* 继承的方法 */

public void SplDoublyLinkedList::add ( mixed $index , mixed $newval )

public mixed SplDoublyLinkedList::bottom ( void )

public int SplDoublyLinkedList::count ( void )

public mixed SplDoublyLinkedList::current ( void )

public int SplDoublyLinkedList::getIteratorMode ( void )

public bool SplDoublyLinkedList::isEmpty ( void )

public mixed SplDoublyLinkedList::key ( void )

public void SplDoublyLinkedList::next ( void )

public bool SplDoublyLinkedList::offsetExists ( mixed $index )

public mixed SplDoublyLinkedList::offsetGet ( mixed $index )

public void SplDoublyLinkedList::offsetSet ( mixed $index , mixed $newval )

public void SplDoublyLinkedList::offsetUnset ( mixed $index )

public mixed SplDoublyLinkedList::pop ( void )

public void SplDoublyLinkedList::prev ( void )

public void SplDoublyLinkedList::push ( mixed $value )

public void SplDoublyLinkedList::rewind ( void )

public string SplDoublyLinkedList::serialize ( void )

public void SplDoublyLinkedList::setIteratorMode ( int $mode )

public mixed SplDoublyLinkedList::shift ( void )

public mixed SplDoublyLinkedList::top ( void )

public void SplDoublyLinkedList::unserialize ( string $serialized )

public void SplDoublyLinkedList::unshift ( mixed $value )

public bool SplDoublyLinkedList::valid ( void )

}

SplQueue::__construct

Constructs a new queue implemented using a doubly linked list

说明

SplQueue::__construct ( 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 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 SplQueue::dequeue ( void )

Dequeues value from the top of the queue.

Note:

SplQueue::dequeue is an alias of class="methodname">SplDoublyLinkedList::shift.

参数

此函数没有参数。

返回值

The value of the dequeued node.

SplQueue::enqueue

Adds an element to the queue

说明

void SplQueue::enqueue ( mixed $value )

Enqueues value at the end of the queue.

Note:

SplQueue::enqueue is an alias of class="methodname">SplDoublyLinkedList::push.

参数

value
The value to enqueue.

返回值

没有返回值。

SplQueue::setIteratorMode

Sets the mode of iteration

说明

void SplQueue::setIteratorMode ( 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 Iterator , Countable {

/* 方法 */

public __construct ( void )

abstract protected int compare ( mixed $value1 , mixed $value2 )

public int count ( void )

public mixed current ( void )

public mixed extract ( void )

public void insert ( mixed $value )

public bool isCorrupted ( void )

public bool isEmpty ( void )

public mixed key ( void )

public void next ( void )

public void recoverFromCorruption ( void )

public void rewind ( void )

public mixed top ( void )

public bool valid ( void )

}

SplHeap::compare

Compare elements in order to place them correctly in the heap while sifting up

说明

abstract protected int SplHeap::compare ( 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 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 SplHeap::__construct ( void )

This constructs a new empty heap.

参数

此函数没有参数。

返回值

没有返回值。

SplHeap::count

Counts the number of elements in the heap

说明

public int SplHeap::count ( void )

参数

此函数没有参数。

返回值

Returns the number of elements in the heap.

SplHeap::current

Return current node pointed by the iterator

说明

public mixed SplHeap::current ( 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 ( 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 ( 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 ( void )

参数

此函数没有参数。

返回值

Returns TRUE if the heap is corrupted, FALSE otherwise.

SplHeap::isEmpty

Checks whether the heap is empty

说明

public bool SplHeap::isEmpty ( void )

参数

此函数没有参数。

返回值

Returns whether the heap is empty.

SplHeap::key

Return current node index

说明

public mixed SplHeap::key ( void )

This function returns the current node index

参数

此函数没有参数。

返回值

The current node index.

SplHeap::next

Move to the next node

说明

public void SplHeap::next ( 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 ( void )

参数

此函数没有参数。

返回值

没有返回值。

SplHeap::rewind

Rewind iterator back to the start (no-op)

说明

public void SplHeap::rewind ( 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 ( 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 ( 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 extends SplHeap implements Iterator , Countable {

/* 方法 */

protected int compare ( mixed $value1 , mixed $value2 )

/* 继承的方法 */

abstract protected int SplHeap::compare ( mixed $value1 , mixed $value2 )

public int SplHeap::count ( void )

public mixed SplHeap::current ( void )

public mixed SplHeap::extract ( void )

public void SplHeap::insert ( mixed $value )

public bool SplHeap::isCorrupted ( void )

public bool SplHeap::isEmpty ( void )

public mixed SplHeap::key ( void )

public void SplHeap::next ( void )

public void SplHeap::recoverFromCorruption ( void )

public void SplHeap::rewind ( void )

public mixed SplHeap::top ( void )

public bool SplHeap::valid ( void )

}

SplMaxHeap::compare

Compare elements in order to place them correctly in the heap while sifting up

说明

protected int SplMaxHeap::compare ( 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 extends SplHeap implements Iterator , Countable {

/* 方法 */

protected int compare ( mixed $value1 , mixed $value2 )

/* 继承的方法 */

abstract protected int SplHeap::compare ( mixed $value1 , mixed $value2 )

public int SplHeap::count ( void )

public mixed SplHeap::current ( void )

public mixed SplHeap::extract ( void )

public void SplHeap::insert ( mixed $value )

public bool SplHeap::isCorrupted ( void )

public bool SplHeap::isEmpty ( void )

public mixed SplHeap::key ( void )

public void SplHeap::next ( void )

public void SplHeap::recoverFromCorruption ( void )

public void SplHeap::rewind ( void )

public mixed SplHeap::top ( void )

public bool SplHeap::valid ( void )

}

SplMinHeap::compare

Compare elements in order to place them correctly in the heap while sifting up

说明

protected int SplMinHeap::compare ( 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 implements Iterator , Countable {

/* 方法 */

public __construct ( void )

public int compare ( mixed $priority1 , mixed $priority2 )

public int count ( void )

public mixed current ( void )

public mixed extract ( void )

public int getExtractFlags ( void )

public bool insert ( mixed $value , mixed $priority )

public bool isCorrupted ( void )

public bool isEmpty ( void )

public mixed key ( void )

public void next ( void )

public void recoverFromCorruption ( void )

public void rewind ( void )

public void setExtractFlags ( int $flags )

public mixed top ( void )

public bool valid ( void )

}

SplPriorityQueue::compare

Compare priorities in order to place elements correctly in the heap while sifting up

说明

public int SplPriorityQueue::compare ( 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 SplPriorityQueue::__construct ( void )

This constructs a new empty queue.

参数

此函数没有参数。

返回值

没有返回值。

SplPriorityQueue::count

Counts the number of elements in the queue

说明

public int SplPriorityQueue::count ( void )

参数

此函数没有参数。

返回值

Returns the number of elements in the queue.

SplPriorityQueue::current

Return current node pointed by the iterator

说明

public mixed SplPriorityQueue::current ( 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 ( 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 SplPriorityQueue::getExtractFlags ( void )

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

SplPriorityQueue::insert

Inserts an element in the queue by sifting it up

说明

public bool SplPriorityQueue::insert ( 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 ( void )

参数

此函数没有参数。

返回值

Returns TRUE if the priority queue is corrupted, FALSE otherwise.

SplPriorityQueue::isEmpty

Checks whether the queue is empty

说明

public bool SplPriorityQueue::isEmpty ( void )

参数

此函数没有参数。

返回值

Returns whether the queue is empty.

SplPriorityQueue::key

Return current node index

说明

public mixed SplPriorityQueue::key ( void )

This function returns the current node index

参数

此函数没有参数。

返回值

The current node index.

SplPriorityQueue::next

Move to the next node

说明

public void SplPriorityQueue::next ( 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 ( 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 SplPriorityQueue::current, SplPriorityQueue::top and SplPriorityQueue::extract.

  • SplPriorityQueue::EXTR_DATA (0x00000001): Extract the data
  • SplPriorityQueue::EXTR_PRIORITY (0x00000002): Extract the priority
  • SplPriorityQueue::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 ( 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 ( 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 implements Iterator , ArrayAccess , Countable {

/* 方法 */

public __construct ([ int $size = 0 ] )

public int count ( void )

public mixed current ( void )

public static SplFixedArray fromArray ( array $array [, bool $save_indexes = TRUE ] )

public int getSize ( void )

public int key ( void )

public void next ( void )

public bool offsetExists ( int $index )

public mixed offsetGet ( int $index )

public void offsetSet ( int $index , mixed $newval )

public void offsetUnset ( int $index )

public void rewind ( void )

public bool setSize ( int $size )

public array toArray ( void )

public bool valid ( void )

public void __wakeup ( 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 SplFixedArray::__construct ([ int $size = 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 SplFixedArray::count ( 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 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 ( 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 static SplFixedArray SplFixedArray::fromArray ( array $array [, bool $save_indexes = TRUE ] )

Import the PHP array array in a new 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 SplFixedArray::getSize ( void )

Gets the size of the array.

参数

此函数没有参数。

返回值

Returns the size of the array, as an integer.

范例

示例 #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 class="methodname">SplFixedArray::count

参见

  • SplFixedArray::count

SplFixedArray::key

Return current array index

说明

public int SplFixedArray::key ( void )

Returns the current array index.

参数

此函数没有参数。

返回值

The current array index.

SplFixedArray::next

Move to next entry

说明

public void SplFixedArray::next ( void )

Move the iterator to the next array entry.

参数

此函数没有参数。

返回值

没有返回值。

SplFixedArray::offsetExists

Returns whether the requested index exists

说明

public bool SplFixedArray::offsetExists ( 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 ( 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 ( 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 ( 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 ( void )

Rewinds the iterator to the beginning.

参数

此函数没有参数。

返回值

没有返回值。

SplFixedArray::setSize

Change the size of an array

说明

public bool SplFixedArray::setSize ( 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 ( 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 ( 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 ( 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 implements Countable , Iterator , Serializable , ArrayAccess {

/* 方法 */

public void addAll ( SplObjectStorage $storage )

public void attach ( object $object [, mixed $data = NULL ] )

public bool contains ( object $object )

public int count ( void )

public object current ( void )

public void detach ( object $object )

public string getHash ( object $object )

public mixed getInfo ( void )

public int key ( void )

public void next ( void )

public bool offsetExists ( object $object )

public mixed offsetGet ( object $object )

public void offsetSet ( object $object [, mixed $data = NULL ] )

public void offsetUnset ( object $object )

public void removeAll ( SplObjectStorage $storage )

public void removeAllExcept ( SplObjectStorage $storage )

public void rewind ( void )

public string serialize ( void )

public void setInfo ( mixed $data )

public void unserialize ( string $serialized )

public bool valid ( 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 ( 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 ( 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"

更新日志

版本说明
5.3.0Added the data parameter.

参见

  • SplObjectStorage::detach
  • SplObjectStorage::offsetSet

SplObjectStorage::contains

Checks if the storage contains a specific object

说明

public bool SplObjectStorage::contains ( 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 SplObjectStorage::count ( 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 ( 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 ( 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 ( object $object )

This method calculates an identifier for the objects added to an SplObjectStorage object.

The implementation in SplObjectStorage returns the same value as 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 ( 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 SplObjectStorage::key ( 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 ( 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 ( 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 ( 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 ( 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 ( 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 ( 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 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 ( 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 ( 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 ( 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 ( 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 ( 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