1.2.1147. Static Methods Cannot Call Non-Static Methods

A static method cannot call a non-static method. The object context would be missing.

On the other hand, a method may call a static method, as the context is lost, but not useful.

Magic methods cannot be static, so they are out of this rule. This applies to the constructor, when called with parent\:\:`__construct() <https://www.php.net/manual/en/language.oop5.decon.php>`_.

<?php

class x {
     function foo() {}

     static function ioo() {
             // This syntax is valid within a class
             // yet, the call is not possible
             self::foo();
     }

}
?>

1.2.1147.1. Suggestions

  • Make the calling method non static too

  • Remove the call to the non-static method

  • Make the target method static

1.2.1147.2. Specs

Short name

Classes/StaticCannotCallNonStatic

Rulesets

All, Analyze, Class Review

Exakat since

2.6.3

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Available in

Entreprise Edition, Exakat Cloud