Home >Backend Development >PHP Tutorial >Why Am I Getting \'Declaration of childClass::customMethod() should be compatible with that of parentClass::customMethod()\'?

Why Am I Getting \'Declaration of childClass::customMethod() should be compatible with that of parentClass::customMethod()\'?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-28 20:28:02723browse

Why Am I Getting

In PHP, Method Declarations Need Compatibility with Parent Methods

PHP programmers might encounter the following error message:

Strict Standards: Declaration of childClass::customMethod() should be compatible with that of parentClass::customMethod()

This error signifies a mismatch between the declaration of a method in a child class and its parent class method declaration.

Possible Causes:

  • Different Arguments: The child class's method customMethod() must have the same number, type, and order of arguments as the parent class's customMethod().
  • Access Level Difference: The child class's method must have the same or higher access level (public/private/protected) as the parent class's method.

Determining Compatibility:

To understand compatibility, consider the following PHP Manual section:

  • [Method Signature Comparison](https://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.method-signature)
  • [Method Overloading](https://www.php.net/manual/en/language.oop5.overloading.php)

These resources explain the criteria for method compatibility, which includes having the same:

  • Method name
  • Number and type of arguments
  • Return type
  • Access level

By addressing these compatibility issues, programmers can eliminate the error and ensure proper inheritance and method overriding.

The above is the detailed content of Why Am I Getting \'Declaration of childClass::customMethod() should be compatible with that of parentClass::customMethod()\'?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn