Introduction to Object-Oriented PHP

This series introduces important concepts in object-oriented PHP. It is authored and produced by our partners at SymfonyCasts (formerly KnpUniversity). In this series, 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.

Tutorials in this course
More information

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

PHP Built-in web server (PHP Manual)

More information

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

The Basics: class (PHP Manual)

More information

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

OOP Basics (PHP Manual)

More information

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

sprintf (PHP Manual)

More information

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

OOP Basics (PHP Manual)

More information

In this PHP tutorial, you'll learn how to create a method that will enable multiple objects to interact with each other.

Additional resources

OOP Basics (PHP Manual)

More information

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

phpDocumentor Tutorial

More information

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

OOP Basics (PHP Manual)

More information

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

Visibility (PHP Manual)

More information

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

Type Hinting (PHP Manual)

More information

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

This course appears in the following guides:
Categories
Module Development, Backend and Infrastructure
Drupal 7, 8, 9, and 10