Plugin Machine | Make Better WordPress Plugins ![rw-book-cover](https://readwise-assets.s3.amazonaws.com/media/uploaded_book_covers/profile_636263/ws7XmnpHnziRKO546GRa4H1znHrMhAwHB2_IqEDOTEo-cover_wSAB0v2.png) [[3. Resources/Readwise/Full Document Contents/Articles/Plugin Machine Make Better WordPress Plugins|Full Document Contents]] ## Document Notes This article outlines how to make better WordPress plugins using Plugin Machine. It provides guidance on making well-defined public APIs, using scalar types, callable, and array types, as well as avoiding potential problems with plugin dependencies by using dependency injection instead of relying on singletons. Plugin Machine can help developers create plugins that are more efficient and easier to deploy, manage, and upgrade. It is important to understand the concepts of public APIs, scalar types, and dependency injection in order to make the most of Plugin Machine and produce higher-quality plugins. - Public APIs: - Replace inner workings of a class without changing classes that use it - PHP7 can return scalar types, callable, or array types - Avoiding Problems: - Instances of plugins must be installed before anything happens - Use dependency injection instead of relying on singletons - Dependency injection allows updating or upgrading plugins without affecting any dependent objects Today, WordPress developers have a powerful new tool at their fingertips: Plugin Machine. This innovative platform allows developers to create better WordPress plugins, taking advantage of the latest advancements in PHP7. With the ability to return scalar types, callables, and array types, developers can create feature-rich plugins that are more reliable and easier to maintain. But Plugin Machine offers more than just improved code. It also enables developers to overcome the challenge of plugin interdependencies. Instead of relying on singletons, Plugin Machine allows developers to use dependency injection, providing objects with their dependencies through another object. This makes it possible to update and upgrade plugins without affecting any dependent objects. Plugin Machine is a revolutionary new way to create better WordPress plugins, giving developers the freedom to create powerful, feature-rich plugins without the worry of dependencies or scalability issues. 1. What are the advantages of using dependency injection? 2. How does dependency injection help with testing hooks? 3. What is the purpose of the base class for REST API endpoints? This document outlines techniques for making better WordPress plugins using object-oriented PHP, dependency injection, and testing. Dependency injection separates common code from the class and allows for outside objects to be passed in. This makes it possible to update or upgrade plugins without affecting any dependent objects. A "Hooks" class is also discussed which does not contain any callbacks, but adds and removes hooks. Dependency injection is also used for REST API controllers and a base class is provided. In addition, tests are recommended in order to ensure that hooks are properly added and removed. > [!note]+ Document Notes > This article outlines how to make better WordPress plugins using Plugin Machine. It provides guidance on making well-defined public APIs, using scalar types, callable, and array types, as well as avoiding potential problems with plugin dependencies by using dependency injection instead of relying on singletons. Plugin Machine can help developers create plugins that are more efficient and easier to deploy, manage, and upgrade. It is important to understand the concepts of public APIs, scalar types, and dependency injection in order to make the most of Plugin Machine and produce higher-quality plugins. > > - Public APIs: > - Replace inner workings of a class without changing classes that use it > - PHP7 can return scalar types, callable, or array types > > - Avoiding Problems: > - Instances of plugins must be installed before anything happens > - Use dependency injection instead of relying on singletons > - Dependency injection allows updating or upgrading plugins without affecting any dependent objects > > Today, WordPress developers have a powerful new tool at their fingertips: Plugin Machine. This innovative platform allows developers to create better WordPress plugins, taking advantage of the latest advancements in PHP7. With the ability to return scalar types, callables, and array types, developers can create feature-rich plugins that are more reliable and easier to maintain. > > But Plugin Machine offers more than just improved code. It also enables developers to overcome the challenge of plugin interdependencies. Instead of relying on singletons, Plugin Machine allows developers to use dependency injection, providing objects with their dependencies through another object. This makes it possible to update and upgrade plugins without affecting any dependent objects. > > Plugin Machine is a revolutionary new way to create better WordPress plugins, giving developers the freedom to create powerful, feature-rich plugins without the worry of dependencies or scalability issues. > > 1. What are the advantages of using dependency injection? > 2. How does dependency injection help with testing hooks? > 3. What is the purpose of the base class for REST API endpoints? > > This document outlines techniques for making better WordPress plugins using object-oriented PHP, dependency injection, and testing. Dependency injection separates common code from the class and allows for outside objects to be passed in. This makes it possible to update or upgrade plugins without affecting any dependent objects. A "Hooks" class is also discussed which does not contain any callbacks, but adds and removes hooks. Dependency injection is also used for REST API controllers and a base class is provided. In addition, tests are recommended in order to ensure that hooks are properly added and removed. ## Highlights - If one instance of the plugin depends on another instance, then both instances must be installed before anything can happen. This makes updating or upgrading the plugin difficult, if not impossible. One way to avoid this problem is to use dependency injection instead of relying on singletons. Dependency injection is a technique where an objectÕs dependencies are provided by another object instead of being hard-coded into the object itself. This makes it possible to update or upgrade plugins without affecting any dependent objects. ([View Highlight](https://read.readwise.io/read/01h0b9ecmkehn03bf300ynzgct)) - Keep in mind that in PHP7, you can return scalar types, callable, or array types. You can not use a class or interface type as a return type until PHP ([View Highlight](https://read.readwise.io/read/01h0b9fts8haeab4g266314vs8)) - e of a function. Make your interfaces as descriptive as possible. The goal here is to make a consumer of classes that use the class not have to know how it works. The arguments of the function should be enough to know what the inputs are. Separation Of Conerns How a class does the thing it does is the concern of the concrete class, and how it interacts with the outside world is the concern of the interface. This separation of concerns is THE idea of OOP. An interface in PHP is a contract that defines a set of method signatures that a class must implement. Her ([View Highlight](https://read.readwise.io/read/01h0b9r1k3p8yfd2zdycvjrg05)) - make well-defined public APIs, where you can replace the inner workings of the class, without changing the classes that use it ([View Highlight](https://read.readwise.io/read/01h0b9k634b7cq7pjhx3qdy426)) - predecessor b2 started on PHP4. Back then the only way to have a global state was through global variables. Global variables are chaos agents and should be avoided in modern PHP. ([View Highlight](https://read.readwise.io/read/01h0ba3302rjejjh9wh7ymdm8n)) - I used static methods to avoid a singleton. I want to have reusable classes, and ensure my refactor doesn’t use singletons. Before getting started, I made sure my tests were passing. ([View Highlight](https://read.readwise.io/read/01h0bacwd9mk44khfhwjk5smk1)) - Anyway, wp_localize_data ([View Highlight](https://read.readwise.io/read/01h0bamem84kmbka0b1hbefm5x)) - Unit Testing: Unit testing is a way to test individual units of code in isolation. In the context of WordPress hooks, this means testing the callback function attached to the hook. You can use phpunit or any other testing framework to write unit tests for your hooks. ([View Highlight](https://read.readwise.io/read/01h0bankg6rw415z9esxz9gyfn)) - Integration Testing: Integration testing is a way to test how different units of code work together. In the context of WordPress hooks, this means testing how the hook interacts with other parts of the WordPress ecosystem, such as other plugins or themes. ([View Highlight](https://read.readwise.io/read/01h0banqhgwyfa60sq2s6x5tqb))