Skip to content

Commit 5b08d55

Browse files
committed
New array class to deal with memory issues.
1 parent 9333088 commit 5b08d55

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

PackedArray.inc

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
*
6+
* Provides a 'packed' array for occasions when PHP array consumes to much memory. Not that this class
7+
* is about twice as slow as PHP arrays, but around three times as compressed. It should only be used when dealing with large arrays where
8+
* processing speed isn't as much of a concern as memory usage.
9+
*
10+
*/
11+
class PackedArray implements ArrayAccess, Serializable {
12+
13+
protected $array = array();
14+
15+
public function __construct() {
16+
$args = func_get_args();
17+
foreach ($args as $k => $v) {
18+
$this->offsetSet($k, $v);
19+
}
20+
}
21+
22+
public function offsetExists($offset) {
23+
return isset($this->array[$offset]);
24+
}
25+
26+
public function offsetGet($offset) {
27+
if ($this->offsetExists($offset)) {
28+
return unserialize($this->array[$offset]);
29+
}
30+
return NULL;
31+
}
32+
33+
public function offsetSet($offset, $val) {
34+
if (isset($offset)) {
35+
$this->array[$offset] = serialize($val);
36+
}
37+
else {
38+
$this->array[] = serialize($val);
39+
}
40+
}
41+
42+
public function offsetUnset($offset) {
43+
unset($this->array[$offset]);
44+
}
45+
46+
public function serialize() {
47+
return serialize($this->array);
48+
}
49+
50+
public function unserialize($serialized) {
51+
$this->array = unserialize($serialized);
52+
}
53+
54+
}

0 commit comments

Comments
 (0)