Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 3 down vote accepted

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!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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