[英]How to refer to a static constant member variable in PHP
I have a class with member variables. 我有一个包含成员变量的类。 What is the syntax in PHP to access the member variables from within the class when the class is being called from a static context?
当从静态上下文调用类时,PHP中用于从类中访问成员变量的语法是什么?
Basically I want to call a class method (but not create a new object), but when the class method is called, I want a handful of static constant variables to be initialized that need to be shared among the different class methods. 基本上我想调用一个类方法(但不是创建一个新对象),但是当调用类方法时,我想要初始化一些需要在不同类方法之间共享的静态常量变量。
OR if there's a better way to do it then what I'm proposing, please share with me (I'm new to PHP) Thanks! 或者,如果有更好的方法,那么我提出的建议,请与我分享(我是PHP的新手)谢谢!
eg. 例如。
class example { var $apple; function example()//constructor { example::apple = "red" //this throws a parse error } }
For brevity sake I will only offer the php 5 version: 为了简洁起见,我将只提供php 5版本:
class Example
{
// Class Constant
const APPLE = 'red';
// Private static member
private static $apple;
public function __construct()
{
print self::APPLE . "\n";
self::$apple = 'red';
}
}
Basically I want to call a class method (but not create a new object), but when the class method is called, I want a handful of static constant variables to be initialized that need to be shared among the different class methods.
基本上我想调用一个类方法(但不是创建一个新对象),但是当调用类方法时,我想要初始化一些需要在不同类方法之间共享的静态常量变量。
Try this 尝试这个
class ClassName {
static $var;
function functionName() {
echo self::$var = 1;
}
}
ClassName::functionName();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.