StaticLambdaBinding
error on by default quick-fix PHP ≥ 5.4Group: Probable bugs · PhpStorm name: StaticLambdaBindingInspection
A static closure or arrow function has no bound object: using $this in it, or calling an instance method of the parent class through parent::, fails at runtime. Either drop static or stop relying on the object.
Example
php
<?php
class Base {
public function render() {}
public static function make() {}
}
class Widget extends Base {
private $label = 'w';
public function handlers() {
return [
function () { return $this->label; },
static function () { return strtoupper($this->label); },
static fn ($s) => $s . $this->label,
static function () { return fn () => 1; },
static function () { return function () { return $this->label; }; },
static function () { return fn () => $this->label; },
static function ($x) { parent::render($x); },
static fn () => parent::render(),
static function () { return parent::make(); },
];
}
}php
<?php
class Base {
public function render() {}
public static function make() {}
}
class Widget extends Base {
private $label = 'w';
public function handlers() {
return [
function () { return $this->label; },
function () { return strtoupper($this->label); },
fn ($s) => $s . $this->label,
static function () { return fn () => 1; },
static function () { return function () { return $this->label; }; },
function () { return fn () => $this->label; },
function ($x) { parent::render($x); },
fn () => parent::render(),
static function () { return parent::make(); },
];
}
}Reported:
- line 13: Static closures have no $this; remove 'static' or avoid $this.
- line 14: Static closures have no $this; remove 'static' or avoid $this.
- line 17: Static closures have no $this; remove 'static' or avoid $this.
- line 18: Calling an instance method of the parent class requires an object; this closure is static.
- line 19: Calling an instance method of the parent class requires an object; this closure is static.
Configure
In custos.json:
json
{
"rules": {
"StaticLambdaBinding": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore StaticLambdaBinding
/**
* @noinspection StaticLambdaBindingInspection
*/