Tutorial for object-oriented programming fundamentals in PHP
This tutorial is intended to help those who are new to object-oriented programming (OOP) and struggling with the fundamentals needed for Drupal 8 development. Drupal 8 is a huge OOP shift compared to the procedural programming of previous versions of Drupal.
Almost everything in this tutorial is specific to PHP, not necessarily Drupal. Any PHP developer may find it helpful, but be aware that some of the examples and application-specific details will be based on Drupal 8.
Many of the topics covered are introductory, and written for a beginner, but you should have some prior PHP experience. Also, this tutorial is not intended to take the place of a generic, introductory OOP tutorial. This is intended to supplement any existing one, and explain some of the details you didn't understand.
This tutorial will not cover all use cases and minute technical details. It aims for general usage.
For the Drupal folks, this is not a module development guide. This will help you in your module development, but do not expect an explanation of plugins, services, and whatnot.
If you are using an IDE, like PHP Storm, it may alert you to problems with some of the classes. This is because it sees the classes in all the directories and may think they are duplicate. The scripts themselves should run fine when executing.
The PHP files provided in this tutorial should be executable, but your results may vary. And, as always, if you find any mistakes, please open an issue or create a pull request. Don't assume I know what I'm doing.™
->
) and double colon (::
) operators$this
and self
__construct
methodsparent
use
statements, and more on the global space