rspec/rules/S1765/php/rule.adoc

59 lines
1.6 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
The `var` keyword in PHP was historically used to declare class properties with default public visibility.
However, its usage is discouraged as it lacks clarity and explicit visibility declaration.
2021-04-28 16:49:39 +02:00
Instead, PHP introduced the keywords `public`, `protected`, and `private` to clearly define the visibility of class properties.
This enhances code readability and maintainability, as it becomes easier to understand and control access to class members.
Additionally, using the keywords for explicit visibility helps prevent unintended modifications or security vulnerabilities that may arise from the ambiguity of the `var` keyword.
2021-04-28 16:49:39 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
[source,php,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
<?php
class Foo
{
var $bar = 1;
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
[source,php,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
<?php
class Foo
{
public $bar = 1;
}
----
== Resources
=== Documentation
* https://www.php.net/manual/en/language.oop5.visibility.php[PHP Manual - Visibility]
* https://www.w3schools.com/php/keyword_var.asp#:~:text=The%20var%20keyword%20creates%20a,public%20should%20be%20used%20instead[W3Schools - var Keyword]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Replace the "var" keyword with the modifier "public".
'''
== Comments And Links
(visible only on this page)
=== on 27 May 2014, 20:01:53 Ann Campbell wrote:
The reference I found has a slightly different reading on the deprecation: \http://www.php.net/manual/en/language.oop5.properties.php
endif::env-github,rspecator-view[]