在类类中的常量var [英] Constants var inside a class class

查看:162
本文介绍了在类类中的常量var的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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";
}

我知道这是不可能的

所以我应该使用它在一个地方有$ url1定义

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";
}

此外,如果我想这样使用, urlOnly只能通过类常量访问。

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

推荐答案

您不能在类定义中使用非标量值。
改为使用 define()

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

这篇关于在类类中的常量var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆