1.2.132. Checks Property Existence¶
This analysis reports checks property existence.
In PHP 8.2, non-specified properties are discouraged : they should always be defined in the class code. When this guideline is applied, properties always exists, and a call to property_exists() is now useless.
Some situations are still legit :
+ When the class is stdClass
, where no property is initially defined. This may be the case of JSON data, or arrays cast to objects.
+ When the class uses magic methods, in particular __get(), __set() and __isset().
In this analysis, isset() and property_exists() are both used to detect this checking behavior. property_exists() is actually the only method to actually check the existence, since isset() will confuse non-existing properties and null
.
While the behavior is deprecated in PHP 8.2, it is recommended to review older code and remove it. It will both ensure forward compatibility and cleaner, faster local code.
<?php
class x {
private $a = 1;
function foo() {
$this->cache = $this->a + 1;
}
}
?>
1.2.132.1. Connex PHP features¶
1.2.132.1.1. Specs¶
Short name |
Classes/ChecksPropertyExistence |
Rulesets |
|
Exakat since |
2.3.3 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
High |
Related rule |
|
Available in |