Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
公共静态函数php pass变量_Php_Function_Static - Fatal编程技术网

公共静态函数php pass变量

公共静态函数php pass变量,php,function,static,Php,Function,Static,我需要将一个变量从一个静态函数传递到同一类中的另一个函数。 我不写完整的代码,我需要理论程序 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 qu

我需要将一个变量从一个静态函数传递到同一类中的另一个函数。 我不写完整的代码,我需要理论程序

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
*/
}

}
注:


不能在静态函数中使用$this

将$one声明为静态变量:

private static $one;

您可以使用:self::$one访问它

您需要在一个类中声明您的变量,然后您可以使用self和范围解析操作符::检索它


您可以将$one定义为类中的静态变量,然后可以在函数2中访问它。一个方法中的变量应该是self::$one=感谢Rahil的双重检查,修复了!
class One {
 private static $one;
 public static function One() {
  self::$one = something_from_your_db();
 }

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