Php patterns

From OrcaQubits/agentic-commerce-skills-plugins · View interactive version

Php patterns is an AI agent skill from the GitHub repository OrcaQubits/agentic-commerce-skills-plugins. Apply PHP design patterns — Singleton, Factory, Strategy, Observer, Repository, Decorator, and Dependency Injection in PHP. Use when structuring WooCommerce extension code or implementing complex business logic.

How to use this skill

Skill file

PHP Design Patterns

Before writing code

Fetch live docs: Web-search php design patterns modern for current pattern implementations. Reference https://refactoring.guru/design-patterns/php for comprehensive examples.

Singleton

What It Does

Ensures only one instance of a class exists. Common in WordPress/WooCommerce plugins as the main plugin class.

WooCommerce Usage

WC() returns the single WooCommerce instance. Extension main classes typically follow:

When to Use

Plugin entry points only. Avoid for everything else — singletons are hard to test and create hidden dependencies.

Factory

What It Does

Creates objects without specifying the exact class. Decouples object creation from usage.

WooCommerce Usage

WC_Product_Factory resolves product type to correct class:

Implementation

Define a factory method or class that maps identifiers to concrete classes. Use for: product types, payment gateway selection, shipping method resolution.

Strategy

What It Does

Defines a family of interchangeable algorithms. Client code works with the interface, not the implementation.

WooCommerce Usage

Implementation

Define an interface/abstract class. Create concrete implementations. Select the strategy at runtime based on configuration or context.

Observer (Hooks)

What It Does

Objects subscribe to events and get notified when they occur. Loose coupling between event producers and consumers.

WooCommerce Usage

WordPress hooks ARE the Observer pattern:

Implementation

Use WordPress hooks for WooCommerce extensions. For internal events within your plugin, consider firing custom actions: do_action( 'my_plugin_after_process', $data ).

Repository

What It Does

Mediates between the domain layer and data mapping layer. Provides collection-like access to data.

WooCommerce Usage

While WooCommerce uses Data Stores rather than formal Repositories, the concept is similar:

Implementation

Create a class that provides methods like find_by_id(), find_all(), save(), delete(). Internally use wc_get_orders() or custom queries.

Decorator

What It Does

Adds behavior to objects dynamically by wrapping them. Each decorator adds one concern.

WooCommerce Usage

Filters act as decorators:

Implementation

Create wrapper classes that implement the same interface as the wrapped object. Delegate most calls, add behavior where needed.

Dependency Injection

What It Does

Objects receive their dependencies through constructors or setters rather than creating them internally.

WooCommerce/WordPress Context

WordPress doesn't have a built-in DI container, but you can practice DI:

Implementation

class OrderProcessor {
    public function __construct(
        private readonly PaymentGateway $gateway,
        private readonly ShippingCalculator $shipping,
    ) {}
}

Service Locator

What It Does

A registry that provides access to services. Less preferred than DI, but practical in WordPress.

WooCommerce Usage

Template Method

What It Does

Defines the skeleton of an algorithm in a base class, letting subclasses override specific steps.

WooCommerce Usage

Best Practices

Fetch design pattern references for current PHP implementations and WooCommerce core examples before implementing.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.