公共静态函数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);
}
}