Follow PSR-4 standards:
src/
├── Core/ # Core plugin functionality
├── Admin/ # WordPress admin area code
├── Frontend/ # Public-facing code
├── Services/ # Business logic and services
├── Interfaces/ # Contracts and interfaces
├── Models/ # Data models
└── Providers/ # Service providers
Classes:
DatabaseService, UserControllerInterface: LoggerInterface, CacheInterfaceAbstract: AbstractProviderNamespaces:
MyPlugin\Services\DatabaseServiceYourVendor\PluginName\...Files:
DatabaseService.phpRegister your services using a service provider src/Providers/PluginServiceProvider pattern for organizing service registration.
1. Don't Store Container Globally
1
2
3
4
5
6
// ❌ Bad
global $my_container;
$my_container = new Container();
// ✅ Good - use the static getInstance() method
$container = Container::getInstance();
2. Don't Use Container as Service Locator
1
2
3
4
5
6
7
8
9
10
11
12
13
// ❌ Bad
class MyService {
public function __construct(Container $container) {
$this->logger = $container->get(LoggerInterface::class);
}
}
// ✅ Good
class MyService {
public function __construct(
private LoggerInterface $logger
) {}
}3. Don't Bind in Random Places
1
2
3
4
5
6
7
8
9
10
11
// ❌ Bad - scattered bindings
add_action('init', function() use ($container) {
$container->bind(SomeService::class);
});
// ✅ Good - centralized service provider
class PluginServiceProvider {
public function register(Container $container): void {
$container->bind(SomeService::class);
}
}