Skip to content

Instantly share code, notes, and snippets.

@FrankM1
Forked from johnpbloch/instance-provider.php
Created March 26, 2013 18:12
Show Gist options
  • Save FrankM1/5247742 to your computer and use it in GitHub Desktop.
Save FrankM1/5247742 to your computer and use it in GitHub Desktop.
<?php
// Here we are, in any scope imaginable. It doesn't matter which
$my_object = new My_Class();
add_filter( 'jpb.provider.my_class', function() use ( $my_object ) {
return $my_object || new My_Class();
} );
// This is in the global scope
function get_my_class_instance() {
$obj = apply_filters( 'jpb.provider.my_class', false );
if( ! $obj ) {
throw new Exception( "This is embarassing, isn't it?" );
}
return $obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment