Automattic\WooCommerce\Proxies
LegacyProxy::get_instance_of
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.
Returns
T. The instance of the class.
Usage
$LegacyProxy = new LegacyProxy(); $LegacyProxy->get_instance_of( $class_name, ...$args );
- $class_name(string) (required)
- Class name.
- ...$args(mixed) (required)
- Parameters to be passed to the class constructor or to the appropriate internal
'get_instance_of_'method.
LegacyProxy::get_instance_of() LegacyProxy::get instance of code WC 10.5.0
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 ' .
'\'init\' method injection or \'wc_get_container()->get()\' 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 );
}