Home > Backend Development > PHP Tutorial > Why Does PHP Throw 'Strict Standards: Declaration of childClass::customMethod() Should Be Compatible with that of parentClass::customMethod()'?

Why Does PHP Throw 'Strict Standards: Declaration of childClass::customMethod() Should Be Compatible with that of parentClass::customMethod()'?

Linda Hamilton
Release: 2024-12-09 21:40:15
Original
389 people have browsed it

Why Does PHP Throw

Compatibility in PHP Method Declarations

In PHP, the error "Strict Standards: Declaration of childClass::customMethod() should be compatible with that of parentClass::customMethod()" indicates a discrepancy between the declaration of a method in a child class and its parent method.

Possible Causes:

The error occurs when the child method has either:

  • Different arguments: The number, order, or data types of the method's arguments should match that of the parent method.
  • Different access level: The access level (public, private, or protected) of the child method must be the same as or less restrictive than that of the parent method.

Finding Compatibility Information:

PHP uses the following rules to define method compatibility:

  • Arguments: Argument declarations must match both in number and type.
  • Access level: Child method access can be more restrictive (e.g., public to private) but not less restrictive (e.g., private to public).
  • Return types: Child method return type can be covariant, meaning it can return a subclass of the parent return type.

Additional documentation and examples on method compatibility can be found in the following PHP Manual pages:

  • [Overloading and Inheritance](https://www.php.net/manual/en/language.oop5.overloading.php)
  • [Method Visibility](https://www.php.net/manual/en/language.oop5.visibility.php)
  • [Covariance and Contravariance](https://www.php.net/manual/en/language.oop5.variance.php)

The above is the detailed content of Why Does PHP Throw 'Strict Standards: 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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template