我想一個類中訪問一個變量的函數:訪問公共靜態變量
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
}
外:google_api_v3 :: $ API_KEY也應努力?和內部使用:self :: $ api_key或static :: $ api_key – 2014-09-04 09:18:38
@RaphaelMüller是的,工作。 – user3467855 2014-09-04 09:21:34
@ user3467855順便說一句,因爲你似乎認爲它是關於可變的可見性,所以你的變量是公有的還是私有的這一事實與你的問題無關。我不明白「它只適用於私人」如何使用$ this-> api_key,它不應該工作。 – Sugar 2014-09-04 09:42:04