Skip to content

Instantly share code, notes, and snippets.

@jakeasmith
Created January 20, 2012 23:02
Show Gist options
  • Save jakeasmith/1650098 to your computer and use it in GitHub Desktop.
Save jakeasmith/1650098 to your computer and use it in GitHub Desktop.
Lazy loading class inside class
<?php
class book
{
// Singular author
private $_author = null;
public function author()
{
if(is_null($this->_author))
{
$this->_author = new Author($this->author_id);
}
return $this->_author;
}
// Multiple authors
private $_authors = array();
public function authors()
{
if(empty($this->_authors))
{
$this->_authors = getAuthorsFromDb();
// getAuthorsFromDb is a bs function for loading up an array of models. replace it with something usable :)
}
return $this->_authors;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment