1

I am using OpenCart and installed the module sizes.xml, it edits the catalog/product/model/catalog/product.php file

It adds a class method;

I have an issue with some code where in a function I have:

class ModelCatalogProduct extends Model {

    public function getSizes($product_id) { 
      ...
    } // end function

} // end class

We always get an error saying that we cannot redeclare getSizes method.

I want to either force redeclaration, or alternatively check whether the function exists before calling the method but I cannot use function_exists in a class.

ie, this seems illegal;

class ModelCatalogProduct extends Model {

    // This throws up an error
    if (function_exists('getSizes')==FALSE) {
       public function getSizes($product_id) {
       } // end function
    }// end if

} // end class

It will throw up an error issue. But I need the function in there.

I would like to use function_exists in this class or alternatively force the method to be redeclared.

How would I make function_exists work inside a class?

6
  • 6
    method_exists() - php.net/manual/en/function.method-exists.php Commented May 30, 2013 at 9:19
  • It would help if you state the actual error you get when declaring the method, rather than an approximation of what it says. Commented May 30, 2013 at 9:20
  • Maybe make this method to be abstract ? Commented May 30, 2013 at 9:20
  • Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION in file ... /vq2-catalog_model_catalog_product.php on line 743 Commented May 30, 2013 at 9:27
  • The error is in a cache file, but I tracked it back to this file. Commented May 30, 2013 at 9:28

2 Answers 2

4

You can't have executable code in a class that is outside of a method, so there's no way you can do what you're asking, as your if() condition would need to be in the class body.

So despite what others are saying, method_exists() is not a suitable answer to this question.

If you're getting an error stating that the method is already declared, then there are a few possible reasons for this:

  1. It is actually already declared elsewhere in the same class. In which case, of course you can't redeclare it. But since the code for the class ought to all be in a single file, then it should be fairly easy to see that and avoid doing it.

  2. It's declared in the parent class (ie in your case Model or one of its parents).

    Under normal circumstances, you should be able to redeclare a method that is already declared in a parent class; your method would override the method of the same name from the parent class. So for most cases, your whole question is entirely unnecessary.

    But you say you're getting errors, so clearly something is going wrong It would help if you'd told us the exact error message, but there are two reasons I can think of this might not work:

    • If the method in the parent class is declared as Final, then it means the author of the parent class explicitly doesn't want it to be overridden. This means that you cannot have your own method of the same name.

    • If the method in the parent class has a different signature - eg it's private in the parent but public in your class, or static in one but not in the other, then you will get errors complaining about that. In this case, you'll need to make sure that the methods have the same signature, or else give your method a different name.

Hope that helps.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes it does, I will check the sizes.xml file to see if the OpenCart module did install a file in the parent
Fatal error: Cannot redeclare ModelCatalogProduct::getSizes() in (directory)/vq2-catalog_model_catalog_product.php on line 762
I have just commented out the method for now and will fix it later
fair enough. I'm curious to know which of the possibilities I mentioned is the cause your issue.
0

The answer of @Spudley is correctly chosen as the right answer.

Just more explanation on the first case mentioned in the answer: if you wand to declare the function inside of a method of a class, you should also consider the namespace of the class:

class MyClass
{
    function myFunction()
    {
         //here, check if the function is defined in the root namespace globally 
         //   or in the current namespace:
         if(!function_exists('someFunction') 
            && !function_exists(__NAMESPACE__ . '\someFunction')) 
         {
              function someFunction()
              {

              }
         }
         //....

         someFunction();
    }
} 

If you don't check the second condition of if, calling myFunction() more than once would throw exception

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.