Having an assignment to “this” can be unproductive

Production RiskSoftware ResiliencyCode Reliability

Having an assignment to “this” can be unproductive

This code insight counts a violation each time the PHP script has an assignment to “this”

“This report lists all the Classes containing assignements to ‘$this’.

It provides the following information:
Class full name.”

Remedy –

Modify the source code and do not assign to ‘$this’.

” <?php
class foo {

function foo(){
// construct
}

function bar(){
$this = true;
}
}
?>

5362

Why you should care

In PHP the assignment to $this is used to unset the object, or it is a typographical error. Unsetting an object is not really necessary for performance reasons. Removing all variables that point to the object to be unset will have the same result. It is also important to remember to do so because the result will be a fatal error. As such, in PHP5 mode, this kind of practice could provoke a failure.

Business Impacts

Production Risk

CAST recommendations

References

https://www.tutorialspoint.com/sql/sql-transactions.htm

5362

About CAST and Highlight’s Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.

See featuresHow it works