2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-05-15 10:44:52 +02:00
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.
2021-04-28 16:49:39 +02:00
2023-10-30 10:33:56 +01:00
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.
2021-04-28 16:49:39 +02:00
2023-05-15 10:44:52 +02:00
Adhering to this convention improves readability and maintainability by ensuring that the constructor declaration is named uniformly throughout the codebase.
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2023-05-15 10:44:52 +02:00
[source,php,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
class Foo {
2023-05-15 10:44:52 +02:00
function Foo() {...}
2021-04-28 16:49:39 +02:00
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2023-05-15 10:44:52 +02:00
[source,php,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
class Foo {
2023-05-15 10:44:52 +02:00
function __construct() {...}
2021-04-28 16:49:39 +02:00
}
----
2021-04-28 18:08:03 +02:00
2023-05-15 10:44:52 +02:00
== 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]
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
2023-05-15 10:44:52 +02:00
2021-09-20 15:38:42 +02:00
== Implementation Specification
2023-05-15 10:44:52 +02:00
2021-09-20 15:38:42 +02:00
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
* Rename this "XXXXX" function to "__construct".
* Replace this function name "XXXXX", since a "__construct" method has already been defined in this class.
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2023-05-15 10:44:52 +02:00
2021-06-02 20:44:38 +02:00
== Comments And Links
2023-05-15 10:44:52 +02:00
2021-06-02 20:44:38 +02:00
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== 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!
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]