Skip to main content
WooCommerce maintains two distinct code organization patterns to balance modern PHP practices with backwards compatibility.

Code Organization

WooCommerce code is split into two main directories:
  • src/: Modern PHP with PSR-4 autoloading and dependency injection
  • includes/: Legacy WordPress patterns for backwards compatibility

Modern Code (src/ Directory)

The src/ directory contains modern PHP code following current best practices.

PSR-4 Autoloading

All classes in src/ use PSR-4 autoloading with proper namespaces:
src/Internal/AssignDefaultCategory.php

Namespace Structure

Automattic\WooCommerce\The base namespace for all WooCommerce code.

Modern Features

Modern code uses contemporary PHP features:
Modern code features:
  • Strict type declarations
  • Type hints for parameters and return values
  • Array type properties (PHP 7.4+)
  • PHPDoc templates for generics
  • Constant visibility modifiers

Dependency Injection

Classes in src/ use constructor injection or the init() method for dependencies:
src/Internal/Admin/Analytics.php

The init() Method Pattern

All classes in src/ that need initialization should implement an init() method:
The init() method should be marked as final and should be public. It’s called automatically by the dependency injection container.

Legacy Code (includes/ Directory)

The includes/ directory maintains backwards compatibility with traditional WordPress patterns.

WordPress Patterns

Legacy code uses classic WordPress coding styles:
includes/class-woocommerce.php

Global Functions

Legacy code includes global helper functions:
includes/wc-template-functions.php

Singleton Pattern

Many legacy classes use the singleton pattern:

Direct WordPress API Calls

Legacy code calls WordPress functions directly:

When to Use Each

Choose the right directory based on what you’re building.

Use src/ For:

1

New Features

All new functionality should be implemented in src/ using modern patterns.
src/Internal/MyNewFeature.php
2

Internal Classes

Classes that are not part of the public API go in src/Internal/.Default location: src/Internal/
3

Testable Code

Code that needs dependency injection for testing.
4

Modern PHP Features

When you need strict types, type hints, or other modern PHP features.

Use includes/ For:

1

Backwards Compatibility

Modifying existing functionality that extensions depend on.
2

Bug Fixes

Fixing bugs in existing legacy code (minimal changes only).
3

Global Functions

Template functions and utilities that themes/plugins call directly.
Minimize changes to includes/ directory. New code should always go in src/.

Migration Approach

When refactoring legacy code to modern patterns:

Step 1: Create Modern Class

Create a new class in src/Internal/:
src/Internal/MyModernClass.php

Step 2: Register with Container

The container automatically resolves classes in the Automattic\WooCommerce namespace.

Step 3: Update Legacy Code

Update legacy code to use the new class:
includes/class-wc-legacy.php

Step 4: Deprecate Legacy Code

Mark old code as deprecated:

Key Differences Summary

Best Practices

1

Default to Modern

Always create new code in src/ unless you have a specific reason not to.
2

Use Internal Namespace

Place new classes in src/Internal/ by default.
3

Implement init() Method

Use the init() method for hook registration and initialization.
4

Use Type Hints

Always use type hints for parameters and return values.
5

Declare Strict Types

Start files with declare( strict_types=1 );
6

Minimal Legacy Changes

Only modify includes/ for critical bug fixes.

Next Steps

Dependency Injection

Learn how to use the DI container in modern code

Architecture

Understand the overall WooCommerce architecture