1

There is a plugin that uses a class; and create an object like this:

class WC_Disability_VAT_Exemption {

    public function __construct() {
        add_action( 'woocommerce_after_order_notes', array( $this, 'exemption_field' ) );
    }

    public function exemption_field() {
        //some code here
    }

}

/**
 * Return instance of WC_Disability_VAT_Exemption.
 *
 * @since 1.3.3
 *
 * @return WC_Disability_VAT_Exemption
 */
function wc_dve() {
    static $instance;

    if ( ! isset( $instance ) ) {
        $instance = new WC_Disability_VAT_Exemption();
    }

    return $instance;
}

wc_dve();

I want to extend the class because I want to use this method to remove an action:

class WC_Disability_VAT_Exemption_Extend extends WC_Disability_VAT_Exemption {

    function __construct() {
        $this->unregister_parent_hook();
        add_action( 'woocommerce_after_order_notes', array( $this, 'exemption_field' ) );
    }

    function unregister_parent_hook() {
        global $instance;
        remove_action( 'woocommerce_after_order_notes', array( $instance, 'exemption_field' ) );
    }

    function exemption_field() {
        //---some code here
    }
}

But global $instance doesn't get the class object. It returns null. So how can I get $instance object in the extended class?

1
  • Your first question's answer is been given. Secondly I don't see you've extended any class here.
    – CodeMascot
    Jun 15 '17 at 9:11
3

Woocommerce support sent me the solution of my problem:

function unregister_parent_hook() {
  if ( function_exists( 'wc_dve' ) ) {
    $instance = wc_dve();
   remove_action( 'woocommerce_after_order_notes', array( $instance, 'exemption_field' ) );
    }
 }
0

Well, you used static key word inside the first function. The keyword static will not make the variable global. It'll make sure that the variable exists only in that local function scope, but it does not lose its value when program execution leaves this scope.

So if you try to access in the second function the global $my_class; it'll obviously return null. Cause PHP will treat the global $my_class; in the second function as a new global variable just declared.

Hope that helps.

3
  • The problem is that the plugin is made by woocommerce and I want to extend it because I want to remove an action. So, should I talk with woocomerce?
    – otinane
    Jun 15 '17 at 9:24
  • Would be better if you give the class a() full code here.
    – CodeMascot
    Jun 15 '17 at 10:40
  • Take a look. I edit the main code above
    – otinane
    Jun 15 '17 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.