0

I get the following error running PHP 5.3.13 and I cannot see why.

Declaration of CustomCourse::toArray() should be compatible with that of BaseCourse::toArray()

This is my PHP code below, although cut down to the important stuff to keep the post length to only what is needed.

I should also add that the Course class exposes no toArray method.

I see other similar threads on SO, but none appear to offer me a solution.

/**
* this is the CHILD class
*/
class CustomCourse extends BaseCourse {

   public function toArray() {
      $values = parent::toArray();
      // do some more with $values here
      return $values;
   }

}

/**
* this is the PARENT class
*/
class BaseContact extends Course {

   public function toArray($platform = GOLF_PLATFORM) {
      $values = array();
      $values['user_id'] = $this->getUserId();
      // do some more in here
      return $values;
   }

}
crmpicco
  • 16,605
  • 26
  • 134
  • 210
  • @bcmcfc The problem with that answer is it doesn't answer my question (see my post). This `childClass::customMethod() has different arguments, or a different access level (public/private/protected) than parentClass::customMethod().` doesn't apply to me. – crmpicco May 20 '13 at 15:34
  • 2
    it does - your two toArray() methods have different signatures: BaseCourse implements a toArray with a parameter and default, CustomCourse overrides it and doesn't have the parameter or default defined in its signature. – bcmcfc May 20 '13 at 15:41
  • 2
    Your child class does not extend your parent class. Is it a typo in the question? And, why do you think the link doesn't answer your question? You've just accepted a quite exact duplicate of such answer. – Álvaro González May 20 '13 at 15:42

1 Answers1

1

It seems that this is a strict error being reported by PHP.

The discussion follows here: Declaration of Methods should be Compatible with Parent Methods in PHP

For the resolution, you will need to use the same declaration for both methods.

class CustomCourse extends BaseCourse {
    function toArray($platform=GOLF_PLATFORM) {
        //do something
    }
}

Alternatively, you can turn off strict error checking in your php.ini file.

Community
  • 1
  • 1
Achrome
  • 7,773
  • 14
  • 36
  • 45
  • Turn off errors is always a bad idea. – Henrique Barcelos May 20 '13 at 18:28
  • Turning off strict errors gets rid of a lot of Notices. Warnings and actual errors should always be kept on. – Achrome May 20 '13 at 18:34
  • IMHO notices aren't displayed for nothing. In this very case, is a conceptual error declare a method within an interface with one signature and then use a different one in the concretion. Since PHP does not support classic method overloading, the signature should always be the same. Your first suggestion is the solution, hide errors NEVER is a solution. – Henrique Barcelos May 20 '13 at 18:38
  • I completely agree. However, I didn't not want to give this option. – Achrome May 20 '13 at 18:40