0

I am not sure if my function is firing before another funciton or if I am having issues with the $this variable when I am extending my class.

I am creating a function in my functions.php file. Here's a link to my function.

The specific function is extending a class that is an extension for WooCommerce. The template page calls the function like so:

<?php
    $ssi_woocommerce_variation_control_output = new SSi_WC_Swatch_Picker( $product->id, $attributes, $variation_params['selected_attributes'] );
    print_r($ssi_woocommerce_variation_control_output);
    $ssi_woocommerce_variation_control_output->picker();
?>

The print_r output will produce the array content to the page. However I get this:

Start the Loop

Notice: Undefined property: SSi_WC_Swatch_Picker::$attributes in /my_url/wp-content/themes/my-theme/functions.php on line 13

Warning: Invalid argument supplied for foreach() in /my_url/wp-content/themes/my-theme/functions.php on line 13

End the Loop

UPDATE Good news (at least for me) the developer of the original class is going to change the private properties to protected and then I can access them without issue.

  • I changed the title to reflect better what was happening. – Ken Dec 30 '14 at 5:31
  • I have also decided based off the properties being private to just extend the class and re-use the entire class and adjust the picker() method to suite my needs. – Ken Dec 30 '14 at 5:33
3

Probably $attribute is declared as private in the super class, so you can't access it directly from subclasses.

  • Yup I looked at the class I'm extending, it has that set to private. So is there nothing I can do then? Would proving the superclass code be beneficial at this point? – Ken Dec 29 '14 at 17:15
  • 1
    If parent class provides getter methods for these variables you have to use them. – jep Dec 29 '14 at 17:47
  • This is quickly spiraling out of my wheelhouse. How would I find out if there are getter methods? I created a paste of the original class here. pastebin.com/EWCn7UCX – Ken Dec 29 '14 at 18:22
  • 1
    Getter methods are methods that returns an object property. If your parent class has these properties set as private and doesn't provide getter method, maybe the author doesn't want let other to manipulate them and you have to find a workaround to deal with them. You can always modify visibility of that attributes to protected ( not private ) but this is dangerous because others can use that class. – jep Dec 29 '14 at 20:01
  • As a little experiment I changed the attributes private to protected and they appeared on the page correctly. Now I need to figure out how to achieve this without editing the core plug-in or if it is even possible. – Ken Dec 29 '14 at 23:10

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.