Created
January 20, 2012 23:02
-
-
Save jakeasmith/1650098 to your computer and use it in GitHub Desktop.
Lazy loading class inside class
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 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