Constants var inside a class class [duplicate]

旧城冷巷雨未停 提交于 2019-12-25 07:56:29

问题


class Constants
{
        public static $url1      = "http=//url1";
        public static $url2       = Constants::$url1."/abc2";
        public static $url3       = Constants::$url1."/abc3";
        public static $url4       = Constants::$url1."/abc4";
}

i know this is not possible

so should i use it like to have $url1 defination at one place

class urlOnly
{
      public static $url1      = "http=//url1";
}
class Constants
{
        public static $url1       = urlOnly::$url1;
        public static $url2       = urlOnly::$url1."/abc2";
        public static $url3       = urlOnly::$url1."/abc3";
        public static $url4       = urlOnly::$url1."/abc4";
}

Also if i want to use like this, can i make this sure that class "urlOnly" can be accessed only by class "Constants".

Alternate solution is most welcomed as in this solution i need to create two classes. Also i want to access variable as a variable only and not as a function and i want this to be accessed like static


回答1:


You can not use non-scalar values in class definition. Use define() instead.




回答2:


One thing you can do to achieve what you're looking for is this:

class Constants {
    public static $url1 = "http://url1";
    public static $url2 = "";
    // etc
}

Constants::$url2 = Constants::$url1 . "/abc2";

Unfortunately, to dynamically define static values you'll have to do so outside of the context of the class because static variables can only be initialized with literals or variables (hence why the previous version of this answer had a parse error).

However, I would recommend the use of define instead as it's purpose is to define constant values and there is no reason to store constants inside the context of a class unless it absolutely makes sense to do so (at least in my opinion).

Something like:

define("URL1", "http:://url1");
define("URL2", URL1 . "/abc2");

Then you have no need to specify a class accessor, just use URL1 or URL2 as needed.




回答3:


Generally there is no way to declare dynamicaly constants and static properties without calling of class methods. But you can implement logic that you want. You should use placeholders in strirngs constants. Then you should add static method "get" for retrieving constants and replacing placeholders. Like this:

class Constants
{
    protected static $config = array(
        'url1' => 'http=//url1',
        'url2' => '%url1%/abc2',
        'url3' => '%url1%/abc3',
        'url4' => '%url1%/abc4',
    );

    public static function get($name, $default = null)
    {
        if (!empty(self::$config[$name]) && is_string(self::$config[$name]) && preg_match('/%(\w[\w\d]+)%/', self::$config[$name], $matches)) {
            self::$config[$name] = str_replace($matches[0], self::$config[$matches[1]], self::$config[$name]);
        }
        return self::$config[$name];
    }
}

How to use:

Constants::get('url1');
Constants::get('url2');
Constants::get('url3');
Constants::get('url4');


来源:https://stackoverflow.com/questions/15963172/constants-var-inside-a-class-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!