LegacyProxy::get_instance_of()publicWC 1.0

Gets an instance of a given legacy class. This must not be used to get instances of classes in the src directory.

If a given class needs a special procedure to get an instance of it, please add a private get_instance_of_(lowercased_class_name) and it will be automatically invoked. See also how objects of classes having a static instance method are retrieved, similar approaches can be used as needed to make use of existing factory methods such as e.g. 'load'.

Method of the class: LegacyProxy{}

No Hooks.


Object. The instance of the class.


$LegacyProxy = new LegacyProxy();
$LegacyProxy->get_instance_of( $class_name, ...$args );
$class_name(string) (required)
The name of the class to get an instance for.
...$args(mixed) (required)
Parameters to be passed to the class constructor or to the appropriate internal 'get_instance_of_' method.

LegacyProxy::get_instance_of() code WC 8.6.1

public function get_instance_of( string $class_name, ...$args ) {
	if ( false !== strpos( $class_name, '\\' ) ) {
		throw new \Exception(
			'The LegacyProxy class is not intended for getting instances of classes in the src directory, please use ' .
			Definition::INJECTION_METHOD . ' method injection or the instance of ' . ContainerInterface::class . ' for that.'

	// If a class has a dedicated method to obtain a instance, use it.
	$method = 'get_instance_of_' . strtolower( $class_name );
	if ( method_exists( __CLASS__, $method ) ) {
		return $this->$method( ...$args );

	// If the class is a singleton, use the "instance" method.
	if ( method_exists( $class_name, 'instance' ) ) {
		return $class_name::instance( ...$args );

	// If the class has a "load" method, use it.
	if ( method_exists( $class_name, 'load' ) ) {
		return $class_name::load( ...$args );

	// Fallback to simply creating a new instance of the class.
	return new $class_name( ...$args );