Way to allow instantiating derived classes I have a class called MyService, a function to get the only instance of it custom_get_my_service(), and multiple functions a,b, etc that call the instance getter, as you can see below.

All of this is packaged into my module. Suppose I give this module to another developer, he can't change the code, but he needs to override some of my class methods, and all the custom_a, custom_b functions have to use his derived class.

How can I modify my custom_get_my_service() function, in a Drupal best practice way, to allow others to instantiate their derived class instead of my own?

class MyService {}

function custom_get_my_service() {
  $service = &amp;drupal_static(__FUNCTION__, NULL);
  if (is_null($service)) {
    $service = new MyService();
  }
  return $service;
}

function custom_a() {
  $service = custom_get_my_service();
  // Do stuff
}

function custom_b() {
  $service = custom_get_my_service();
  // Do stuff
}
You can do it however you like, really; however PHP will let you. 

Basically you need to make a decision at some point in custom_get_my_service() about which class you're going to use. Drupal's paradigm is hooks, so a contrived example might be:

function custom_get_my_service() {
  $service = &amp;drupal_static(__FUNCTION__, NULL);
  if (is_null($service)) {
    $service_class = custom_get_service_class();
    $service = new $service_class();
  }
  return $service;
}

function custom_get_service_class() {
  // Default
  $class = 'MyService';

  $modules = module_implements('custom_service_class');
  foreach ($modules as $module) {
    $function = "{$module}_custom_service_class";
    $class = $function();
  }

  return $class;
}


Then another module would implement the hook:

function another_module_custom_service_class() {
  return 'AnotherClass';
}


Obviously that's a very limited example, which will only let one module override the class, has race conditions, no error checking, etc. But that's the basic idea, I'm sure you can extrapolate from there based on what your module is providing.

You could also look at a DI approach, and have modules pass in their implementations of an interface you define into your functions, which then make the necessary use of them. It kind of depends on your level of experience with OO, and your opinions on 'best practice'.