68 lines
1.2 KiB
Plaintext
68 lines
1.2 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)
|
|
|
|
include::message.adoc[]
|
|
|
|
'''
|
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|