Skip to content

Instantly share code, notes, and snippets.

@tbcorr
Last active September 24, 2015 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbcorr/bc3e98682e2b4c23a04a to your computer and use it in GitHub Desktop.
Save tbcorr/bc3e98682e2b4c23a04a to your computer and use it in GitHub Desktop.
Lazy Instantiation
<?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