WooCommerce Code Reference

Container
in package

PSR11 compliant dependency injection container for WooCommerce.

Classes in the src directory should specify dependencies from that directory via an 'init' method having arguments with type hints. If an instance of the container itself is needed, the type hint to use is \Psr\Container\ContainerInterface.

Classes in the src directory should interact with anything outside (especially code in the includes directory and WordPress functions) by using the classes in the Proxies directory. The exception is idempotent functions (e.g. wp_parse_url), those can be used directly.

Classes in the includes directory should use the wc_get_container function to get the instance of the container when they need to get an instance of a class from the src directory.

Internally, an instance of RuntimeContainer will be used for the actual class resolution. This class uses reflection to instantiate classes and figure out dependencies, so there's no need for explicit class registration. When running the unit tests suite this will be replaced with an instance of TestingContainer, which provides additional functionality.

Table of Contents

$container  : RuntimeContainer
The underlying container.
__construct()  : mixed
Class constructor.
get()  : object
Returns an instance of the specified class.
has()  : bool
Returns true if the container can return an instance of the given class or false otherwise.

Properties

$container

The underlying container.

private RuntimeContainer $container

Methods

__construct()

Class constructor.

public __construct() : mixed
Return values
mixed

get()

Returns an instance of the specified class.

public get(string $id) : object

See the comment about ContainerException in RuntimeContainer::get.

Parameters
$id : string

Class name.

Tags
throws
ContainerException

Error when resolving the class to an object instance, or class not found.

throws
Exception

Exception thrown in the constructor or in the 'init' method of one of the resolved classes.

Return values
objectObject instance.

has()

Returns true if the container can return an instance of the given class or false otherwise.

public has(class-string $id) : bool

See the comment in RuntimeContainer::has.

Parameters
$id : class-string

Class name.

Return values
bool