I have a User class which needs to load data from a variety of sources, e.g. the main user database, WordPress user metadata, authorizations from another app, etc. For the sake of organization, I want to separate out those different data loaders into their own classes. Calls to $user->get_name() would appropriately delegate down to whatever backend object provides this information.

How might this look in PHP? Is there a name for this pattern? (And hopefully it's not an anti-pattern.)

I can anticipate people suggesting some embedded objects, like $user->wordpress as an interface to the WordPress loader. I have an additional complicating factor, that not all users are present in all of our data stores. My loaders are able to identify if they understand and have data for a given username/id, and they're loaded into the User object with a priority level. A given identifier may only trigger a subset of data loaders, so $user->get_name() essentially is a consistent interface to user data, wherever it's stored.

I'm evaluating my setup in an effort to make things more testable, and I believe this is an appropriate first step.

1 Answer 1


Hey maybe you could implement a Composite pattern, looking something like:

alt text

The component execute() method could call al its datasources and so on till you reach a basicDatasource witch may have another hireachy, representing each source.

Now back to you needs, the get_name() function could look like:

public function get_name()
  $datasource = $this->getDataSourcesForName();
  return $datasource->execute();

Also a strategy patter may apply.

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.