0

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?

1
  • two ways either SQL::$partsNetTotalPounds or self::$partsNetTotalPounds Commented Jul 5, 2013 at 8:01

3 Answers 3

3
public static function margin()
  {
    return "(" . self::$partsNetTotalPounds ;
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I had forgotten about that keyword.
0

you need to use self keyword

self::$partsNetTotalPounds

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.