rspec/rules/S2014/php/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

101 lines
2.8 KiB
Plaintext

== Why is this an issue?
To reference the current class instance the keyword $this can be used.
Through the use of this keyword you have access to class properties and methods.
Static methods can be accessed without instantiating the class, so `$this` is not available for them.
Using `$this` in a static context will result in a runtime error.
== How to fix it
=== Code examples
==== Noncompliant code example
[source,php,diff-id=1,diff-type=noncompliant]
----
class Clazz {
public $name = NULL; // instance variable
public static function foo() {
if ($this->name != NULL) {
// ...
}
}
}
----
==== Compliant solution
[source,php,diff-id=1,diff-type=compliant]
----
class Clazz {
public $name = NULL; // instance variable
public static function foo($nameParam) {
if ($nameParam != NULL) {
// ...
}
}
}
----
== Resources
=== Documentation
* https://www.php.net/manual/en/language.oop5.basic.php[PHP Manual - Classes and Objects Basics]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove this use of "$this".
'''
== Comments And Links
(visible only on this page)
=== on 17 Sep 2014, 10:33:13 Freddy Mallet wrote:
@Ann, if my feeling is correct the "Compliant Code Example" is incorrect because the $name field is not declared as being static. Moreover are you sure that when you call a static method on a class instance, the $this keyword is really correctly interpreted in the body of the static method ? (I'm asking the question because for sure in Java, this is not the case)
=== on 19 Sep 2014, 07:16:47 Ann Campbell wrote:
\[~freddy.mallet] the compliant code example has $name being passed in to the static method as a parameter. I've renamed it to make that clearer. And no, ``++$this++`` doesn't work correctly in a static context.
=== on 12 Oct 2014, 18:17:04 Freddy Mallet wrote:
@Ann, I guess I know why I was a bit lost when discovering this description :
____
But static methods can be accessed without instantiating the class, so it's very possible that there is no class instance at the time the code is executed, resulting in runtime errors.
____
According to this description, sounds like it's possible to access to a class instance from a static function whereas this is indeed not at all possible.
So I would rework the description to prevent any misunderstanding.
=== on 13 Oct 2014, 15:00:43 Linda Martin wrote:
\[~ann.campbell.2] +1 for what Freddy said.
If it can help here is what php interpreter error says: ``++"Fatal error: Using $this when not in object context [...]".++``
=== on 13 Oct 2014, 15:13:12 Ann Campbell wrote:
is this any better [~linda.martin]?
=== on 13 Oct 2014, 17:00:10 Linda Martin wrote:
Yes! Thanks [~ann.campbell.2]!
endif::env-github,rspecator-view[]