1

I need to pass a variable from a static function to another within the same class. I do not write the full code, I need the theoretical procedure

enter code here

class One
{

public static function One()
{
/**
*  some code extract from DB $one
*/
}

public static function two()
{
/**
*  I need to retrieve the variable $one to use it in another query DB
*/
}

}

Note:

you can't use $this in a static function

1
  • You can define $one as a static variable in the class, then you will be able to access it in function two Commented Feb 19, 2014 at 9:36

2 Answers 2

2

Declare $one as a static variable:

private static $one;

And you can access it using : self::$one

Sign up to request clarification or add additional context in comments.

Comments

1

You need to declare your variable within your One class, then you can retrieve it using self and the scope resolution operator ::.

class One {
 private static $one;
 public static function One() {
  self::$one = something_from_your_db();
 }

 public static function two() {
  do_something(self::$one);
 }
}

2 Comments

the variable within one() method should be self::$one =
Thanks Rahil for the double check, fixed!

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.