php - 在函数php中访问类内的公共(public)静态变量

标签 php class oop

我正在尝试访问函数中的类中的变量:

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
}

最佳答案

使用值/方法(包括静态方法)的通用方式里面 类(class)是self::

echo self::$api_key;

关于php - 在函数php中访问类内的公共(public)静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25661615/

相关文章:

oop - golang插件编程

php - 在 PHP 中使用带有动态变量名称的大括号

php - 匹配单词的任何组合

php - 不在对象上下文调用类中的方法时使用 $this

c++ - 如何在 C++ 中将类传递给类?

java - 网格布局不起作用

c++ - "Exchange"包括

php - 从表中查询记录,其中 attribute = items from array from previous query

php - 获取所有未关注用户 2 的用户

python - Python中的类对象和类类型