Last active
September 24, 2015 16:04
-
-
Save tbcorr/bc3e98682e2b4c23a04a to your computer and use it in GitHub Desktop.
Lazy Instantiation
This file contains 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 LazyInstantiation { | |
// we can only access this static property | |
// through the public static getter since | |
// the property is private | |
private static $expensive_to_compute; | |
// public static getter | |
public static function get_expensive_to_compute(){ | |
if( self::$expensive_to_compute === null ){ // or empty() | |
// only instantiate once | |
self::$expensive_to_compute = some_function_that_is_expensive(); | |
} | |
return self::$expensive_to_compute; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment