PHP Service Classes
FreeIn this course, we're going to continue on from the Introduction to Object-Oriented PHP series. We're working on the same spaceship project: it has ships, you choose them, then they engage in epic battle!
In an editor, far far away, you'll see a simple application that runs this: index.php is the homepage and battle.php does the magic and shows the results. In the first course, we created a single class called Ship
, which describes all its properties—it's like a container for one ship's details. In this tutorial we're going to replace our flat functions and create a BattleManager
service class to provide the methods we'll need to do that.
This course introduces important concepts in object-oriented PHP. It is authored and produced by our partners at KnpUniversity (now SymfonyCasts). In this PHP course, you'll be building a PHP app using PHP and refactoring the code, step-by-step, using concepts in OO-PHP such as classes, methods, access control, type hinting, and constructors. You'll learn how to have one object interact with another and by the end of this project, your PHP app will be sporting some shiny new object-oriented PHP.
In this lesson, Leanna introduces you to the project and shows you how to get it up and running. So, look for the Course code download link below and we'll walk you through the process of getting the app up and running on your computer using the built-in PHP server. As long as you have PHP installed on your computer and a code or text editor, you should be able to complete the lessons in this series. (A full stack web server (i.e. Apache/MySQL/PHP) is not required, only PHP.) Follow along by running commands from the start
directory.
Additional resources
This PHP tutorial covers the basics of classes and objects. You'll learn how to set up a class and then what a class is and what objects are like. By the end of this tutorial you should be able to create a class, an object, create a property, and set the value of a property inside a class.
Additional resources
In this PHP tutorial, you'll learn about methods — functions that live inside objects. You'll also learn how to access properties inside methods using the $this
pseudo-variable.
Additional resources
In this PHP tutorial, you'll get more practice working with methods. Along with adding some functionality to a method using PHP's sprintf()
function to format a string, you'll use the $this
pseudo-variable, and learn how to add and use arguments with methods.
Additional resources
In this PHP tutorial you'll learn how to work with multiple object instances of the same class that have different data and function independently.
Additional resources
In this PHP tutorial, you'll learn how to create a method that will enable multiple objects to interact with each other.
Additional resources
In this PHP tutorial, you'll learn how to add a specially-formatted comment to your method that will enable additional autocomplete functionality in IDEs such as PHPStorm.
Additional resources
In this PHP tutorial, you'll learn how to refactor the practice code in play.php into the Ship class. You'll get more practice working inside of a class and with objects.
Additional resources
In this PHP tutorial, you'll learn about to change the visibility of properties to private
and how this affects how these properties can be accessed. You'll change the public properties to private and then add "getter" and "setter" methods to the class to enable controlled access of the values of these private properties.
Note: the word "hooks" in this video does not refer to hooks in Drupal's API.
Note: PHP does provide magic methods for getting and setting which are explained in this tutorial: Magic Methods: __toString(), __get, __set.
Additional resources
In this PHP tutorial, you'll learn how Type Hinting can help you get better errors from PHP as you develop your application. You'll learn to debug errors and see how Type Hinting impacts the phrasing of the error messages you receive.
Additional resources
In this PHP tutorial, you'll learn how to use Constructors to set up objects and perform certain operations when a new object of this type is instantiated.
Additional resources
Constructors and Destructors (PHP Manual)
Explore more PHP tutorials
In this PHP tutorial we are going to continue to remove the flat functions in our code. We'll refactor one of them to a private function and create a new ShipLoader
service class to clean up the rest. With this refactoring in place, we'll be able to rename the functions.php file since it won't contain any functions any longer.
The next thing we want to tackle is our battle()
function, with its array, inside our BattleManager class. It's not obvious at all what's inside the $outcome
variable, or whether the keys it has now might be missing or different in the future. To rectify this we're going to create a new BattleResult model class with some properties and methods that will clean things up and remove the need for weird associative arrays. Our BattleManager::battle()
function will return a nice BattleResult object, and we'll be in full control of what public methods we put on that.
We now have a BattleResult
class, and we type-hinted the two Ship
arguments. But now, if you look at the battle()
function, there's a case where the ships can destroy each other. When that happens, there is no winning or losing ship—they're both null. Since null
is not a Ship
object, PHP gets angry and throws an error. In this PHP tutorial we'll fix that problem by creating a isThereAWinner
semantic method in our BattleResult
class.
In this PHP tutorial we're going to update some information in our ship object and see another way that objects are different from arrays—objects are always passed by reference. We need to add a new feature to our app so that after the battle we can display the final health of the battling ships. One will be zero or negative, but how much health did the other have left? Let's take a look at how we can update the ships to reflect their new health after a battle.
So far we've been hard-coding our ships in the ShipLoader
class. In this tutorial we'll take things up a notch by fetching that information from a database. First we're going to create a new database to hold our data, using PDO
. (If you are not familiar with working with databases in PHP, you can check out the PHP for Beginners Part 3 series, which covers database fun.) Once we have the database created and filled with some ship data, we'll learn how to fetch that data back out again as an object our app can work with.
In the prevous tutorial we set up our app to fetch the ship objects from the database, but now our app ship select lists are not working properly. To deal with that, in this PHP tutorial, we're going to add a ship ID property to our class and then we will use the ShipLoader
class to query for individual ships. Lastly, we will once again need to turn the array that we fetch into an object that our app can use. As an added bonus we'll also update the PHPDoc so we can get autocompleting method names.
Our database is working well, but we currently have things set up so we are duplicating the database information, and that is going to be a nightmare to maintain. In this tutorial, we're going to clean up our database code by isolating the PDO
creation in the ShipLoader
class. Even with that in place though, we also need to make sure we only return one PDO
object. We don't need a new database connection every time it gets called on a page. We'll fix that with a little bit of logic in our PDO
code.
Things are working pretty well, but we still have some things to clean up. In this tutorial we're going to review some best practices. Our current problem is that at the bottom of ShipLoader
, our database connection information is hard-coded. That's a problem for two reasons. First, if this works on my computer, it probably won't work on production, unless everything matches up. Secondly, what if we need a database connection inside some other class? Right now, we'd just have to copy and paste those credentials into yet another spot, which is definitely not ideal.
Here's the goal: move the database configuration out of this class to somewhere more central so it can be re-used. The way you do this is fundamentally important to using object-oriented code correctly. The concept we are going to implement here is called dependency injection. The idea here is don't put configuration inside of a service class. Replace that hard-coded configuration with an argument. This allows anyone using your class to pass in whatever they want. The hard-coding is gone, and your class is more flexible.
Additional resources
Dependency Injection and the Art of Services and Containers series