SPL-StandardPHPLibrary
Public Member Functions | Private Attributes | List of all members
SplObjectStorage Class Reference
Inheritance diagram for SplObjectStorage:
Inheritance graph
Collaboration diagram for SplObjectStorage:
Collaboration graph

Public Member Functions

 attach ($obj, $inf=NULL)
 
 contains ($obj)
 
 count ()
 
 current ()
 
 detach ($obj)
 
 getInfo ()
 
 key ()
 
 next ()
 
 offsetEsists ($obj)
 
 offsetExists ($offset)
 
 offsetGet ($obj)
 
 offsetSet ($obj, $inf)
 
 offsetUnset ($obj)
 
 rewind ()
 
 setInfo ($inf=NULL)
 
 valid ()
 

Private Attributes

 $index = 0
 
 $storage = array()
 

Detailed Description

Object storage.

Author
Marcus Boerger
Version
1.1
Since
PHP 5.1.2

This container allows to store objects uniquly without the need to compare them one by one. This is only possible internally. The code representation here therefore has a complexity of O(n) while the actual implementation has complexity O(1).

Definition at line 23 of file splobjectstorage.inc.

Member Function Documentation

SplObjectStorage::attach (   $obj,
  $inf = NULL 
)
Parameters
$objnew object to attach to storage or object whose associative information is to be replaced
$infassociative information stored along the object

Definition at line 113 of file splobjectstorage.inc.

References contains().

Referenced by offsetSet().

114  {
115  if (is_object($obj) && !$this->contains($obj))
116  {
117  $this->storage[] = array($obj, $inf);
118  }
119  }

Here is the call graph for this function:

SplObjectStorage::contains (   $obj)
Parameters
$objobject to look for
Returns
whether $obj is contained in storage

Definition at line 94 of file splobjectstorage.inc.

Referenced by attach(), and offsetEsists().

95  {
96  if (is_object($obj))
97  {
98  foreach($this->storage as $element)
99  {
100  if ($object === $element[0])
101  {
102  return true;
103  }
104  }
105  }
106  return false;
107  }
SplObjectStorage::count ( )
Returns
number of objects in storage

Implements Countable.

Definition at line 86 of file splobjectstorage.inc.

87  {
88  return count($this->storage);
89  }
SplObjectStorage::current ( )
Returns
current object

Implements Iterator.

Definition at line 51 of file splobjectstorage.inc.

Referenced by getInfo().

52  {
53  $element = current($this->storage);
54  return $element ? $element[0] : NULL
55  }
SplObjectStorage::detach (   $obj)
Parameters
$objobject to remove from storage

Definition at line 123 of file splobjectstorage.inc.

References rewind().

Referenced by offsetUnset().

124  {
125  if (is_object($obj))
126  {
127  foreach($this->storage as $idx => $element)
128  {
129  if ($object === $element[0])
130  {
131  unset($this->storage[$idx]);
132  $this->rewind();
133  return;
134  }
135  }
136  }
137  }
rewind()
Rewind to top iterator as set in constructor.

Here is the call graph for this function:

SplObjectStorage::getInfo ( )
Returns
get current object's associated information
Since
5.3.0

Definition at line 60 of file splobjectstorage.inc.

References current().

61  {
62  $element = current($this->storage);
63  return $element ? $element[1] : NULL
64  }

Here is the call graph for this function:

SplObjectStorage::key ( )
Returns
current key

Implements Iterator.

Definition at line 44 of file splobjectstorage.inc.

References $index.

Referenced by valid().

45  {
46  return $this->index;
47  }
SplObjectStorage::next ( )

Forward to next element.

Implements Iterator.

Definition at line 78 of file splobjectstorage.inc.

79  {
80  next($this->storage);
81  $this->index++;
82  }
next()
Forward to next element.
SplObjectStorage::offsetEsists (   $obj)
Parameters
$objobject to look for
Returns
whether $obj is contained in storage

Definition at line 182 of file splobjectstorage.inc.

References contains().

183  {
184  return $this->contains($obj);
185  }

Here is the call graph for this function:

ArrayAccess::offsetExists (   $offset)
inherited
Parameters
$offsetto check
Returns
whether the offset exists.

Implemented in ArrayIterator, ArrayObject, SplDoublyLinkedList, and DbaArray.

SplObjectStorage::offsetGet (   $obj)
Parameters
$objExising object to look for
Returns
associative information stored with object
Exceptions
UnexpectedValueExceptionif Object $obj is not contained in storage
Since
5.3.0

Implements ArrayAccess.

Definition at line 155 of file splobjectstorage.inc.

156  {
157  if (is_object($obj))
158  {
159  foreach($this->storage as $idx => $element)
160  {
161  if ($object === $element[0])
162  {
163  return $element[1];
164  }
165  }
166  }
167  throw new UnexpectedValueException('Object not found');
168  }
Exception thrown to indicate an unexpected value.
Definition: spl.php:478
SplObjectStorage::offsetSet (   $obj,
  $inf 
)
Parameters
$objnew object to attach to storage or object whose associative information is to be replaced
$infassociative information stored along the object
Since
5.3.0

Implements ArrayAccess.

Definition at line 144 of file splobjectstorage.inc.

References attach().

145  {
146  $this->attach($obj, $inf);
147  }
attach($obj, $inf=NULL)

Here is the call graph for this function:

SplObjectStorage::offsetUnset (   $obj)
Parameters
$objExising object to look for
Returns
associative information stored with object
Since
5.3.0

Implements ArrayAccess.

Definition at line 174 of file splobjectstorage.inc.

References detach().

175  {
176  $this->detach($obj);
177  }

Here is the call graph for this function:

SplObjectStorage::rewind ( )

Rewind to top iterator as set in constructor.

Implements Iterator.

Definition at line 30 of file splobjectstorage.inc.

Referenced by detach().

31  {
32  rewind($this->storage);
33  }
rewind()
Rewind to top iterator as set in constructor.
SplObjectStorage::setInfo (   $inf = NULL)
Returns
set current object's associated information
Since
5.3.0

Definition at line 69 of file splobjectstorage.inc.

References $index, and valid().

70  {
71  if ($this->valid()) {
72  $this->storage[$this->index][1] = $inf;
73  }
74  }

Here is the call graph for this function:

SplObjectStorage::valid ( )
Returns
whether iterator is valid

Implements Iterator.

Definition at line 37 of file splobjectstorage.inc.

References key().

Referenced by setInfo().

38  {
39  return key($this->storage) !== false;
40  }

Here is the call graph for this function:

Member Data Documentation

SplObjectStorage::$index = 0
private

Definition at line 26 of file splobjectstorage.inc.

Referenced by key(), and setInfo().

SplObjectStorage::$storage = array()
private

Definition at line 25 of file splobjectstorage.inc.


The documentation for this class was generated from the following file: