Another potentially significant change seen in PHP 8 is that several methods are now declared static. If we are already using the classes and methods described here as direct object instances, then we do not have a problem.

The following methods are now declared as static:

  • tidy::repairString()

  • tidy::repairFile()

  • XMLReader::open()

  • XMLReader::xml()

The potential for a code break may occur if we override one of the classes mentioned previously. In this case, we must declare the overridden method as static.

Working with the static return type

The static keyword is used in several contexts in PHP. Its basic uses are beyond the scope of this discussion. In this section, we will focus on a new usage for static as a return data type.

Since static is considered a subtype of self, it can be used to widen the narrower return type of self. The static keyword cannot be used as a type hint, however, as it would violate the Liskov Substitution Principle. It would also confuse developers because static is already used in too many other contexts.

The most common use for this new return data type would be in classes that use the fluent interface. This a technique whereby an object method returns an instance of the current object state, thus allowing a chain of method calls to be used in a fluent (readable) manner. In the following example, note how the object builds an SQL SELECT statement:

Get hands-on with 1200+ tech skills courses.