Skip to content

Instantly share code, notes, and snippets.

@gcoop
Created October 20, 2010 16:07
Show Gist options
  • Save gcoop/636719 to your computer and use it in GitHub Desktop.
Save gcoop/636719 to your computer and use it in GitHub Desktop.
<?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