Question

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?

Était-ce utile?

La solution

public static function margin()
  {
    return "(" . self::$partsNetTotalPounds ;
  }

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top