ClassicPress / classic-commerce

A simple but powerful e-commerce platform built for ClassicPress. Forked from WooCommerce and compatible with many Woo extensions.
https://classiccommerce.cc/
GNU General Public License v3.0
50 stars 15 forks source link

Constructing objects using "$some_object = new Some_Class()" makes subclassing difficult. #324

Closed magenta-cuda closed 2 years ago

magenta-cuda commented 3 years ago

I often want to extend a ClassicCommerce class Some_Class. However, this is sometimes made difficult to do because ClassicCommerce sometimes constructs objects of Some_Class by "$some_object = new Some_Class();". I will explain the difficulty using the method prepare_attributes() of class WC_Meta_Box_Product_Data as an example.

public static function prepare_attributes( $data = false ) {
    ...
    $attribute = new WC_Product_Attribute();
    ...
}

I have extended the class WC_Product_Attribute:

class SV_Product_Attribute extends WC_Product_Attribute {
}

However, my extension will not be constructed since MC_Product_Attribute WC_Meta_Box_Product_Data::method prepare_attributes() calls "new WC_Product_Attribute()". An easy totally backward compatible solution would be:

public static function WC_Meta_Box_Product_Data::prepare_attributes( $data = false ) {
    ...
    $classname = apply_filters( 'woocommerce_product_attribute_class', 'WC_Product_Attribute', $data );
    $attribute = new $classname();
    ...
}