【发布时间】:2014-09-04 09:16:37
【问题描述】:
我正在尝试在函数中访问类中的变量:
class google_api_v3 {
public static $api_key = 'this is a string';
function send_response() {
// access here $api_key, I tried with $this->api_key, but that works only on private and that variable I need also to access it outside the class that is why I need it public.
}
}
function outside_class() {
$object = new google_api_v3;
// works accessing it with $object::api_key
}
【问题讨论】:
-
outside: google_api_v3::$api_key 应该也可以吗?和内部使用:self::$api_key 或 static::$api_key
-
@RaphaelMüller 是的,正在工作。
-
@user3467855 顺便说一句,由于您似乎认为它与变量可见性有关,因此您的变量是公共的还是私有的这一事实与您的问题无关。我不明白 $this->api_key 是如何“只在私有上工作”的,它不应该工作。