Developer Advisory: Inheritance in PHP 8

tl;dr

Inheritance errors due to incompatible method signatures will generate fatal errors in PHP 8, which may cause developer plugins to break.  You should check your code for classes that extend functionality in their ancestor with methods that don’t have a matching signature and update those functions.

The details

In PHP 7, inheritance errors due to incompatible method signatures (Liskov Substitution Principle violations) previously generated a warning in some cases.  As of PHP 8, these violations will generate a fatal error, which may cause your code to stop working.  You can read more about this in the PHP 8 Upgrading Guide or in the related RFC.

PHP 8 is scheduled to be released on November 26th, 2020.  The WordPress Core contributors are working to make sure WordPress 5.6, which is scheduled to be released December 8th, 2020, supports PHP 8, but the minimum required version of PHP is not changing at this time.  If you’re interested in learning more about the broader PHP 8 compatibility testing efforts for WordPress, you can find more information here.

How can I tell if my code is affected by this?

Our internal teams did some testing to get a better understanding of what is and what is not allowed under the new rules.  We recommend you take a look at the code in your extension to see if it is affected.

What is allowed:

  • A subclass can add or change a default value; a base class can be modified to remove or change a default value.
  • A subclass can add a new parameter with a default value; a base class can be modified to remove a parameter that had a default value.
  • A subclass can loosen or remove a parameter type declaration; a base class can be modified to add a parameter type declaration or make it more strict.
    • This raises warnings in versions before 7.4, but it still works.
  • A subclass can add a return type declaration; a base class can be modified to remove a return type declaration.
  • In PHP 7.4+, a subclass can make a return type declaration stricter; a base class can be modified to make it looser.

What isn’t allowed:

  • A subclass cannot remove a default value; a base class cannot be modified to add a default value to an existing parameter without one.
  • A subclass cannot remove a parameter even if the base class declared a default value; a base class cannot be modified to add a new parameter, even with a default value.
  • A subclass cannot add a parameter type declaration or make one more strict; a base class cannot be modified to remove a parameter type declaration or make it looser.
  • A subclass cannot loosen or remove a return type declaration; a base class cannot be modified to add a return type declaration or make it more strict.
  • In PHP 7.0–7.3, a subclass cannot make a return type declaration stricter; a base class cannot be modified to make it looser.

Developer Tip

If you’re not already, using phpcs to lint your PHP files can help give peace of mind that your code will remain compatible with new PHP versions and that any breaking changes require minimal intervention to maintain their functionality long-term.


As we learn more about the release of PHP 8 and its potential effects on the WooCommerce developer community, we’ll be sure to keep you updated here.  In the meantime, don’t hesitate to reach out to us in the comments section below or in the #developers channel of our WooCommerce Community Slack.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.