Module Development
Topic

Object-Oriented PHP for Drupal 8, 9, 10, and 11

Object-oriented PHP utilizes classes and objects to organize code into reusable chunks. This approach helps us organize complex applications, such as Drupal, into modular code called classes that can be reused across the entire system. As a module developer, knowledge of object-oriented PHP is essential in order to understand the architecture of a module and the various forms it can take as well as how to implement custom functionality.

Example tasks

Confidence

Learning modern object-oriented PHP practices is a skill that will benefit you not only as a Drupal developer but also as a PHP developer. If you have a doubt about a certain feature working on your project, check php.net to see which versions of PHP are supported. Also, verify your version of PHP on your localhost as well as your web host.

Drupalize.Me resources

Posted on Tuesday, August 12, 2014 - 09:00 by joe

As a developer, one of the first new concepts you're going to need to understand for Drupal 8 is PSR-4. Or, the PHP Framework Interoperability Groups (php-fig), Proposal for Standard Request number 4 (PSR-4). A lot of fancy naming, abbreviations and numbers for a pretty simple idea. Allow your PHP classes to be automatically discovered and loaded by following a convention for class namespace and file/directory naming.

Categories
Drupal 10, 11
More information

PHP attributes are a native PHP language feature, introduced in PHP 8.0, that provide a way to add metadata to classes, methods, properties, and functions in PHP code.

In Drupal, this metadata is used by the plugin system to aid in the discovery and configuration of plugin instances. As a Drupal developer, it's important to understand how to recognize, read, and write PHP attributes, as you'll encounter them when working with plugins.

In this tutorial we'll look at:

  • What PHP attributes are
  • The use case for attributes in Drupal
  • An overview of the attribute syntax

By the end of this tutorial, you should understand how attributes are used in Drupal and how to write them in your own code.

More information

This reference document contains a detailed list of available properties for the #[ContentEntityType] attribute in Drupal 11. The current documentation in the Drupal 11 code base lists, but doesn't always describe, each of these properties. Or, in many cases, the property is an associative array where the nested key/value pairs are important, but what they should be isn't well documented. So we've created this helpful reference. Most of the properties listed here will also apply to #[ConfigEntityType] attributes. These properties define the metadata and behavior of custom entity types.

In this tutorial, we'll:

  • Look at an example of how to use #[ContentEntityType] attributes.
  • Provide a comprehensive list of available properties and a description of each.
  • Show detailed examples of using nested array properties like handlers[].

By the end of this tutorial, you should be able to understand how and when to use any of the properties of a #[ContentEntityType] attribute in order to modify the behavior of a custom entity type.

Categories
Drupal 8, 9, 10, and 11
More information

Annotations are specially-formatted PHP docblock comments that are used for class discovery and metadata description. While it is technically possible to use annotations for other purposes, at the moment Drupal only uses them for the plugin system.

In this tutorial we'll look at:

  • What annotations are
  • The use case for annotations
  • How to figure out what you can put into an annotation

By the end of this tutorial you should understand how annotations are used in Drupal and how to write them in your own code.

Categories
Drupal 8, 9, 10, and 11
More information

Dependency injection is a design pattern commonly used in object-oriented software architectures in order to support Inversion of Control.

PHP

Topic
Drupal 7, 8, 9, 10, and 11
More information

PHP: Hypertext Preprocessor (PHP) is a popular interpreted programming language that is especially suited for web development. Drupal core and all contributed modules are written in PHP.

More information

Drupal core uses object-oriented programming (OOP). This method of programming introduces new coding standards to the project. In this tutorial we'll cover the Drupal coding standards that relate to object-oriented PHP code.

By the end of this tutorial you should know how to implement the Drupal coding standards related to OOP, and where to find more information when you've got questions about the standards.

Services

Topic
Categories
Drupal 8, 9, 10, and 11
More information

Services are objects that encapsulate the code for performing specific tasks in a reusable and decoupled way.

Related guides

Categories
Module Development, Backend and Infrastructure
Drupal 8, 9, 10, and 11
Categories
Site Building, Theming, Module Development
Drupal 10, 11
Categories
Module Development, Backend and Infrastructure
Drupal 10, 11

Guides

Not sure where to start? Our guides provide useful learning tracks for all skill levels.

Navigate guides

External resources