简体   繁体   English

我可以在类中设置常量然后在PHP外部访问它吗?

[英]Can I set a constant in a class then access it outside in PHP?

I am trying to initialize some values inside a class and save them in constant and access them outside, in different part of my code. 我正在尝试初始化类中的一些值并将它们保存在常量中并在我的代码的不同部分中访问它们。

<?php

class Config {

  public static function initialize() {
    define('TEST',"This is a Constant");
  }

}

$config = Config::initialize();
// do something with the constants

Can I access it outside? 我可以在外面访问吗?

A Class constant uses the const keyword. Class常量使用const关键字。 You don't define them using the define function. 您没有使用define函数定义它们。 Just like this: 像这样:

class Config {
        const TEST = "This is a constant";
}

// then use it:
var_dump(Config::TEST);

In PHP, you cannot dynamically set the value of a constant, but you can get a similar behaviour with a public static variable. 在PHP中,您无法动态设置常量的值,但您可以使用公共静态变量获得类似的行为。 ie. 即。

class Config2 {
    public static $test = null;
    public static function initialize()
    {
        self::$test = "This is not a constant";
    }
}

// Then use like
Config2::initialize();
var_dump(Config2::$test);

The downside is, there is nothing stopping other code from setting the value from outside the class. 缺点是,没有什么能阻止其他代码从类外部设置值。 If you need protection against this, you should use a getter function approach. 如果需要对此进行保护,则应使用getter函数方法。 eg. 例如。

class Config3 {
    private static $_test = null;
    public static function initialize()
    {
        self::$_test = "This is not a constant, but can't be changed outside this class";
    }

    public static function getTest()
    {
        return self::$_test;
    }
}

// Then use like
Config3::initialize();
var_dump(Config3::getTest());
class Config {
  const TEST = "This is a Constant";

  static public $test = "This is a static property test."
  static protected $test2;

  public static function initialize() {
      self::$test2 = 'initialized';
      return self::$test2;
  }

  public static function getTest2()
  {
      return self::$test2;
  }
}

echo Config::TEST; // outputs This is a Constant
echo Config::$test; // outputs This is a static property test.
echo Config::initialize(); // outputs initialized;
echo Config::getTest2(); // outputs initialized;

If you need to dynamically set the value then you dont want to use a constant you want to use a static property. 如果需要动态设置值,则不希望使用常量来使用静态属性。 IF you only want the Config class to be able to manipulate the value of this property directly then use the private or protected keyword. 如果您只希望Config类能够直接操作此属性的值,则使用privateprotected关键字。 If thats a non issue then you could use a public property. 如果这是一个非问题,那么你可以使用public财产。

Another and perhaps most robust approach depending on what you are trying to implement is to use constants to access static or instance specific properties of the class: 另一种可能最强大的方法取决于您尝试实现的方法是使用常量来访问类的静态或实例特定属性:

class Config
{
  const TEST = 0;
  const TEST2 = 1;

  protected static $conf = array();

  public static function initialize($testVal, $test2Val)
  {
    $conf[self::TEST] = $testVal;
    $conf[self::TEST2] = $test2Val;
  }

  public static function get($key)
  {
     if(isset(self::$conf[$key])
     {
       return self::$conf[$key];
     }
  }
}

Config::initialize('Test', 'Test 2');
echo Config::get(Config::TEST);
echo Config::get(Config::TEST2);

Not from your original code. 不是来自原始代码。 But the following would work as a constant class variable. 但是以下内容可以作为常量类变量。

class Config {
    const TEST = "This is a Class Constant";
...
}

Accessed from anywhere that includes the Config class declaration like: 从包含Config类声明的任何地方访问,如:

echo Config::TEST;

是的 - 您可以将其作为Config::TEST访问

在这种情况下,您可能只需要一个带有getter方法或类常量的private变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM