Created
October 20, 2010 16:07
-
-
Save gcoop/636719 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FooBar { | |
private $id = 1; | |
private $user; | |
public function getUser() { | |
if (!$this->user instanceof User) { // Only load it if we don’t have it. | |
$this->user = DB::load(‘User’, $this->id); // Cache the result in the object. | |
} | |
return $this->user; // Return cache (new/old who cares). | |
} | |
function __sleep() { | |
return array(‘id’); // Exclude the internal User object from serealization (may become stale, let DB handle single object caching). | |
} | |
function __wakeup() { | |
$this->getUser(); // Throw result, but primes the cached User object. | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment