Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

leestar's avatar

Using the Facebook PHP SDK the right way (SOLID architecture)

I've installed the Facebook PHP SDK in my project via Composer. I am thinking about the best way to include the class(es) in my Laravel project. Two approaches come to mind:

  1. Use the classes directly. Dependency inject each class as needed, and create binds for each class as needed in a service provider if they need to be constructed a special way.

  2. Create a SocialNetworkProviderInterface and FacebookSocialNetworkProvider class, which will be injected with the Facebook PHP SDK dependencies.

The second seems like too many "layering" of classes, but has the advantage of keeping app code consistent if, say, the Facebook PHP SDK changes. Any opinions?

0 likes
3 replies
accent-interactive's avatar
Level 14

Write out the code as you would like it to work, and then create a domain FacebookService class that does just that, using the Facebook SDK.

If in the future you ever decide to change away from the current Facebook SDK, you can always rename your FacebookService class to FacebookServiceSdk, create an FacebookService interface based on that domain class and bind it to a new implementation. That way, you only have to touch the Facebook class and the rest of your code will still work.

A SocialNetworkProviderInterface would probably be too generic, as you need different functionalities for different networks (e.g. Twitter does not have events, Facebook does not have retweets, etc) and the available returned data will also differ per framework. Unless you only use functionality thta is similar beteeen networks (like oauth) this calls for a different class per framework.

leestar's avatar

This makes plenty of sense. I guess the hardest part in figuring out what to abstract away is in figuring out what is likely to change or be swapped - in this case it makes 100% sense to abstract away Facebook itself since plenty of SDKs exist for it, even in the same language.

Thanks for the insight!

accent-interactive's avatar

You're welcome! It can be a real brainbreaker sometimes, and there is no 'right' answer either :)

Please or to participate in this conversation.