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.

没有正确的解决方案

许可以下: CC-BY-SA归因
scroll top