In part 4 of this series on object-oriented PHP from SymfonyCasts (formerly KnpUniversity), we'll explore static methods, namespaces, exceptions, and traits. Getting a handle on these concepts will not only help you with PHP app development, but will also help you understand what's going on under-the-hood of Drupal 8.
Finally, we can really see all the cool OOP tools and concepts that we haven't covered yet. These are the last missing pieces that are standing between you and understanding any OO code you meet.
- Static methods
- Class constants
- Autoloading (RIP require statements)
- __toString and the other magic functions
- Treat that
$objectlike an array (ArrayAccess)
- Throwing and catching Exceptions
- Traits! Horizontal code re-use
- Composition Versus Inheritance: Dressing your classes for success
Other object-oriented PHP courses you might want to check out first:
You might also want to check out our guide, PHP Fundamentals for more PHP video tutorials.
In this PHP tutorial, you'll get the project files up and running and learn all about class constants in object-oriented PHP.
If you're serious about getting really good at this stuff, code along with me. To do that, download the source code from this page, unzip it, and move into the start directory. When you do that, you'll have the same code that I have here. Open up the README file and follow the instructions inside to get things setup.
When that's done, open your favorite terminal application, move into the directory, and — like we've done in the previous courses — start the built-in php web server by running:
php -S localhost:8000
This is a great server to use for development. Then, in our browser, we can go to http://localhost:8000. Here is our beautiful Battles app!
In this PHP tutorial, we'll continue learning about static methods and when to use static vs. non-static methods.
In this PHP tutorial, you'll learn about namespaces and the
use statement in PHP.
In this PHP tutorial, learn about what an autoloader is and how you can use it to replace
require statements in your PHP applications.
In this PHP tutorial, learn how the
use statement works with the autoloader to replace the
require statements that we removed in a previous lesson.
In this PHP tutorial we'll continue our look at namespaces and how they work in PHP applications.
In this tutorial, learn about how to set up your app to use Composer Autoloading.
In this PHP tutorial we'll introduce Exceptions in PHP.
In this PHP tutorial, you'll learn about the different exception classes that are available to you use in your PHP applications.
In this tutorial, learn about PHP's built-in magic methods:
In this tutorial, learn how to use PHP's built-in interface
ArrayAccess and when you might want to use it.
In this tutorial, learn how to implement PHP's built-in interface
IteratorAggregate to loop over an object.
In this PHP tutorial, we'll introduce Traits in PHP and how you can utilize them to reuse your code.
In modern PHP, you're going to spend a lot of time working with other people's classes: via external libraries that you bring into your project to get things done faster. Of course, when you do that: you can't actually edit their code if you need to change or add some behavior.
Fortunately, object-oriented code gives us some really neat ways to deal with this limitation. In this tutorial, you'll learn a method called composition in which we'll create a wrapper class, which has some subtle advantages over using inheritance.
Object-Oriented PHP (Topic) (Drupalize.Me)