rspec/rules/S1603/php/rule.adoc
Fred Tingaud d3cfe19d7e
Fix broken or dangerous backquotes
Co-authored-by: Marco Borgeaud <89914223+marco-antognini-sonarsource@users.noreply.github.com>
2023-10-30 10:33:56 +01:00

66 lines
1.8 KiB
Plaintext

== Why is this an issue?
Using a function in PHP with the same name as the nesting class was historically used to declare a class constructor.
However, as of PHP 8.0.0, this declaration is discouraged and will provoke an `E_DEPRECATED` error, albeit it functions as a constructor.
Instead, users should explicitly define the constructor by declaring a ``++__construct(...)++`` function.
However, if both styles are present in the same class, PHP will treat the ``++__construct++`` function as the class constructor, which can cause unintended behavior.
Adhering to this convention improves readability and maintainability by ensuring that the constructor declaration is named uniformly throughout the codebase.
=== Noncompliant code example
[source,php,diff-id=1,diff-type=noncompliant]
----
class Foo {
function Foo() {...}
}
----
=== Compliant solution
[source,php,diff-id=1,diff-type=compliant]
----
class Foo {
function __construct() {...}
}
----
== Resources
=== Documentation
* https://www.php.net/manual/en/language.oop5.decon.php[PHP Manual - Constructors and Destructors]
* https://www.phptutorial.net/php-oop/php-constructors/[PHP Tutorial - Constructors]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Rename this "XXXXX" function to "__construct".
* Replace this function name "XXXXX", since a "__construct" method has already been defined in this class.
'''
== Comments And Links
(visible only on this page)
=== on 23 Feb 2014, 23:19:58 Freddy Mallet wrote:
Is implemented by \http://jira.codehaus.org/browse/SONARPLUGINS-3513 for PHP
=== on 12 Oct 2015, 12:35:59 Ann Campbell wrote:
\[~linda.martin] I've updated your edits. Double-check me.
=== on 13 Oct 2015, 10:18:57 Linda Martin wrote:
\[~ann.campbell.2] Perfect thanks!
endif::env-github,rspecator-view[]