1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.memory.memory-objects">
4 <title>Memory Objects</title>
6 <sect2 id="zend.memory.memory-objects.movable">
10 Create movable memory objects using the <methodname>create([$data])</methodname>
11 method of the memory manager:
14 <programlisting language="php"><![CDATA[
15 $memObject = $memoryManager->create($data);
19 "Movable" means that such objects may be swapped and unloaded from
20 memory and then loaded when application code accesses the object.
24 <sect2 id="zend.memory.memory-objects.locked">
28 Create locked memory objects using the <methodname>createLocked([$data])</methodname>
29 method of the memory manager:
32 <programlisting language="php"><![CDATA[
33 $memObject = $memoryManager->createLocked($data);
37 "Locked" means that such objects are never swapped and unloaded
42 Locked objects provides the same interface as movable objects
43 (<classname>Zend_Memory_Container_Interface</classname>).
44 So locked object can be used in any place instead of movable objects.
48 It's useful if an application or developer can decide, that some
49 objects should never be swapped, based on performance considerations.
53 Access to locked objects is faster, because the memory manager doesn't
54 need to track changes for these objects.
58 The locked objects class (<classname>Zend_Memory_Container_Locked</classname>)
59 guarantees virtually the same performance as working with a string
60 variable. The overhead is a single dereference to get the class property.
64 <sect2 id="zend.memory.memory-objects.value">
65 <title>Memory container 'value' property</title>
68 Use the memory container (movable or locked) '<code>value</code>'
69 property to operate with memory object data:
72 <programlisting language="php"><![CDATA[
73 $memObject = $memoryManager->create($data);
75 echo $memObject->value;
77 $memObject->value = $newValue;
79 $memObject->value[$index] = '_';
81 echo ord($memObject->value[$index1]);
83 $memObject->value = substr($memObject->value, $start, $length);
87 An alternative way to access memory object data is to use the <link
88 linkend="zend.memory.memory-objects.api.getRef"><methodname>getRef()</methodname></link>
89 method. This method <emphasis>must</emphasis> be used for <acronym>PHP</acronym>
90 versions before 5.2. It also may have to be used in some other
91 cases for performance reasons.
95 <sect2 id="zend.memory.memory-objects.api">
96 <title>Memory container interface</title>
99 Memory container provides the following methods:
102 <sect3 id="zend.memory.memory-objects.api.getRef">
103 <title>getRef() method</title>
105 <programlisting language="php"><![CDATA[
106 public function &getRef();
110 The <methodname>getRef()</methodname> method returns reference to the object value.
114 Movable objects are loaded from the cache at this moment if the
115 object is not already in memory. If the object is loaded from
116 the cache, this might cause swapping of other objects if the
117 memory limit would be exceeded by having all the managed
122 The <methodname>getRef()</methodname> method <emphasis>must</emphasis> be
123 used to access memory object data for <acronym>PHP</acronym> versions before 5.2.
127 Tracking changes to data needs additional resources.
128 The <methodname>getRef()</methodname> method returns reference to string,
129 which is changed directly by user application.
130 So, it's a good idea to use the <methodname>getRef()</methodname> method
131 for value data processing:
134 <programlisting language="php"><![CDATA[
135 $memObject = $memoryManager->create($data);
137 $value = &$memObject->getRef();
139 for ($count = 0; $count < strlen($value); $count++) {
140 $char = $value[$count];
146 <sect3 id="zend.memory.memory-objects.api.touch">
147 <title>touch() method</title>
149 <programlisting language="php"><![CDATA[
150 public function touch();
154 The <methodname>touch()</methodname> method should be used in common with
155 <methodname>getRef()</methodname>. It signals that object value has been changed:
158 <programlisting language="php"><![CDATA[
159 $memObject = $memoryManager->create($data);
162 $value = &$memObject->getRef();
164 for ($count = 0; $count < strlen($value); $count++) {
167 $value[$count] = $char;
176 <sect3 id="zend.memory.memory-objects.api.lock">
177 <title>lock() method</title>
179 <programlisting language="php"><![CDATA[
180 public function lock();
184 The <methodname>lock()</methodname> methods locks object in memory.
185 It should be used to prevent swapping of some objects you choose.
186 Normally, this is not necessary, because the memory manager uses
187 an intelligent algorithm to choose candidates for swapping.
188 But if you exactly know, that at this part of code some
189 objects should not be swapped, you may lock them.
193 Locking objects in memory also guarantees that reference
194 returned by the <methodname>getRef()</methodname> method is valid until you
198 <programlisting language="php"><![CDATA[
199 $memObject1 = $memoryManager->create($data1);
200 $memObject2 = $memoryManager->create($data2);
206 $value1 = &$memObject1->getRef();
207 $value2 = &$memObject2->getRef();
209 for ($count = 0; $count < strlen($value2); $count++) {
210 $value1 .= $value2[$count];
213 $memObject1->touch();
214 $memObject1->unlock();
215 $memObject2->unlock();
219 <sect3 id="zend.memory.memory-objects.api.unlock">
220 <title>unlock() method</title>
222 <programlisting language="php"><![CDATA[
223 public function unlock();
227 <methodname>unlock()</methodname> method unlocks object when it's no longer
228 necessary to be locked. See the example above.
232 <sect3 id="zend.memory.memory-objects.api.isLocked">
233 <title>isLocked() method</title>
235 <programlisting language="php"><![CDATA[
236 public function isLocked();
240 The <methodname>isLocked()</methodname> method can be used to check if
241 object is locked. It returns <constant>TRUE</constant> if the object
242 is locked, or <constant>FALSE</constant> if it is not locked.
243 This is always <constant>TRUE</constant> for "locked" objects,
244 and may be either <constant>TRUE</constant> or <constant>FALSE</constant>
245 for "movable" objects.