<?php
class Foo {
protected $_value;
public function __construct(){
return $this->_value = 1;
}
public static function getValue(){
return $this->_value * 5;
}
}
echo Foo::getValue();
As per php documentation,
Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside methods declared as static.
https://www.php.net/manual/en/language.oop5.static.php
In the above code snippet, static method getValue() will throw the error this is the same use case.
Things to be noted in Static Method and Static Variable:
Static properties are accessed using the Scope Resolution Operator (::
) and cannot be accessed through the object operator (->
).
It’s possible to reference the class using a variable. The variable’s value cannot be a keyword (e.g. self
, parent
and static
).