public static function margin()
{
return "(" . self::$partsNetTotalPounds ;
}
Use static variable in static function
-
02-06-2022 - |
Pregunta
How do I reference a static variable from within a static function in the same class?
I am trying:
class SQL {
public static $partsNetTotalPounds = '...';
public static function margin()
{
return '('.$this->partsNetTotalPounds...
}
}
Ofcourse this does not work because I haven't instantiated the object SQL.
How can this be done?
Solución
Otros consejos
you need to use self
keyword
self::$partsNetTotalPounds
Like the other people have pointed out use the self
keyword:
self::$partsNetTotalPounds
.
If the value of the $partsNetTotalPounds
is all you need you don't need to access it through the margin
method, by the way. Instead you can access it via SQL::$partsNetTotalPounds
.
$this
is used to access properties of an instance so it does not apply in this case.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow