Related to the previous tutorial where we implemented best practices to centralize our configuration, we also want to minimize the number of database connections. We want one connection that every class uses. In this tutorial we'll move the new PDO()
call out of ShipLoader
so that it can be created in a central location and used by everyone. How? By using the same strategy we just learned with configuration. If you want to move something out of a service class, add it as a __construct()
argument and pass it in.
So far we have to create our service objects by hand and this stuff is duplicated. We need to centralize our service objects to make our lives easier. To do that, in this tutorial we'll create one special class whose only job is to create these service objects. This class is called a service container because, well, it's basically a container for all the service objects. We're going to create our service container and then update our code to use it properly.
To wrap things up with our app, the last bit of housekeeping is to make one container responsible for creating every service object, like PDO, but also ShipLoader
and BattleManager
. The problem is that if we called $container->getPDO()
twice on the same request, we'd still end up with multiple PDO
objects, and so, multiple database connections. Ok, if we're careful, we can avoid this. We can do better though—let's guarantee that only one PDO
object is ever created. We did this previously in ShipLoader
, so now we'll move this into our container.
We've got a nice little app working now, using our service container well. Good work! In this final lesson for this series we're going to have a review of working with containers. We'll look at why this is such a good practice, discuss model classes versus service classes, and take a moment to acknowledge best practices versus the real world implementations you can end up with.
Additional resources
In this series, we’ll continue to build the spaceship app that we’ve started in Introduction to Object-Oriented PHP and Object-Oriented PHP Part 2. In this tutorial, I’ll show you how to use the extends
keyword in PHP so that you can use it to inherit properties and methods from another class
.
Additional resources
In this tutorial, I will show you how to override a method that you’ve inherited from another class. By having two classes we are starting to shape the different behaviors and properties of each, while still keeping most things in common and not duplicated.
In this tutorial, I will show you the difference between public, private, and protected methods or properties in PHP classes. By the end of this lesson you will learn how to make things private at first, protected once you need to access them in a subclass, and public when you need to use it outside of its class and subclass.
Additional resources
In this tutorial, I will show you how to get around the problem of calling a function that you have overridden in your class: how to call the parent class method.
Additional resources
In this tutorial, I will show you how we can build a better blueprint and hierarchical structure for our app’s classes through the use of abstract classes.
Additional resources
In this tutorial, I will show you the power of abstract classes and how you can enforce subclasses to define certain functions. In this way, you can share logic but for things that are really specific to subclasses, you can force them to define certain functions, but with the specific logic required.
Additional resources
In this tutorial, I will show you how to extend an abstract class and ensure that all the methods that are required by the abstract class are included in this new class.
Additional resources
In this tutorial, I will show you how to handle new requirements for data storage by creating two smaller abstract classes out of our original data storage class. By creating these new abstract classes, we can build more flexibility into our code.
Additional resources
Now that we have created new abstract classes, we need to load and utilize them in our code. In this tutorial, I will show you how we can refactor our code to make use of our new abstract data loading classes.
We have an abstract class with methods that we require, but there’s no logic in these abstract methods. This is a perfect opportunity to use interfaces. In this tutorial, I will show you how to create and implement Interfaces. I will also explain how interfaces are useful when you will be sharing your code. But even if you’re not creating interfaces to share, chances are, if you’re using open source code — and definitely in Drupal — you will need to know how to implement interfaces.