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'.

Метод класса: LegacyProxy{}

Хуков нет.


Объект. The instance of the class.


$LegacyProxy = new LegacyProxy();
$LegacyProxy->get_instance_of( $class_name, ...$args );
$class_name(строка) (обязательный)
The name of the class to get an instance for.
...$args(разное) (обязательный)
Parameters to be passed to the class constructor or to the appropriate internal 'get_instance_of_' method.

Код LegacyProxy::get_instance_of() WC 9.0.2

public function get_instance_of( string $class_name, ...$args ) {
	if ( StringUtil::starts_with( $class_name, 'Automattic\\WooCommerce\\' ) ) {
		throw new \Exception(
			'The LegacyProxy class is not intended for getting instances of classes whose namespace starts with \'Automattic\\WooCommerce\', 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 );