Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Item
- {
- private $key;
- private $expirations;
- public function __construct($key, ArrayObject $expirations)
- {
- $this->key = $key;
- $this->expirations = $expirations;
- }
- public function __destruct()
- {
- if (isset($this->expirations[$this->key])) {
- unset($this->expirations[$this->key]);
- }
- }
- public function getKey()
- {
- return $this->key;
- }
- public function setExpiration($expiration)
- {
- $this->expirations[$this->key] = $expiration;
- }
- }
- class Pool
- {
- private $expirations;
- public function __construct()
- {
- $this->expirations = new ArrayObject();
- }
- public function getItem($key)
- {
- return new Item($key, $this->expirations);
- }
- public function save(Item $item)
- {
- $key = $item->getKey();
- if (isset($this->expirations[$key])) {
- echo 'Item ' . $key . ' expires at: ' . $this->expirations[$key] . PHP_EOL;
- } else {
- echo 'Item ' . $key . ' expiry not set' . PHP_EOL;
- }
- }
- }
- // Test it works
- $pool = new Pool();
- $foo = $pool->getItem('foo');
- $bar = $pool->getItem('bar');
- $foo->setExpiration(100);
- $pool->save($foo); // Item foo expires at 100
- $pool->save($bar); // Item bar expiry not set
- // Check for protection against memory leak
- $foo2 = clone $foo;
- unset($foo);
- $pool->save($foo2); // Item foo expiry not set
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement