-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathMemoize.php
More file actions
56 lines (49 loc) · 1.26 KB
/
Memoize.php
File metadata and controls
56 lines (49 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* PHP Command Line Tools
*
* This source file is subject to the MIT license that is bundled
* with this package in the file LICENSE.
*
* @author James Logsdon <dwarf@girsbrain.org>
* @copyright 2010 James Logsdom (http://girsbrain.org)
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace cli;
abstract class Memoize {
/** @var array<string, mixed> */
protected $_memoCache = array();
/**
* Magic getter to retrieve memoized properties.
*
* @param string $name Property name.
* @return mixed
*/
public function __get($name) {
if (isset($this->_memoCache[$name])) {
return $this->_memoCache[$name];
}
// Hide probable private methods
if (0 == strncmp($name, '_', 1)) {
return ($this->_memoCache[$name] = null);
}
if (!method_exists($this, $name)) {
return ($this->_memoCache[$name] = null);
}
($this->_memoCache[$name] = $this->$name());
return $this->_memoCache[$name];
}
/**
* Unmemoize a property or all properties.
*
* @param string|bool $name Property name to unmemoize, or true to unmemoize all.
* @return void
*/
protected function _unmemo($name) {
if ($name === true) {
$this->_memoCache = array();
} else {
unset($this->_memoCache[$name]);
}
}
}