未解析基本语法的解决方法 [英] Workaround for basic syntax not being parsed

查看:27
本文介绍了未解析基本语法的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个类属性,允许在等号右侧进行表达式.所有版本的 PHP 都依赖于以下代码,但它是这样编写的,以便将来更容易扩展.

I want to have a class property that allow for an expression to take place on the right side of the equals sign. All versions of PHP choke on the following code, but it is written in this way to allow for easier extendibility in the future.

/* Example SDK Class */
class SDK
{
    /* Runtime Option Flags */
    // Strings
    #  0: Makes no change to the strings.
    var $STRING_NONE        = (1 << 0);
    #  1: Removes color codes from the string.
    var $STRING_STRIP_COLOR = (1 << 1);
    #  2: Removes language codes from the string.
    var $STRING_STRIP_LANG  = (1 << 2);
    #  3: Removes all formatting from the string.
    var $STRING_STRIP       = SELF::STRING_STRIP_COLOR & SELF::STRING_STRIP_LANG;
    #  4: Converts color codes to HTML & UTF-8.
    var $STRING_HTML        = (1 << 3);
    #  8: Converts color codes to ECMA-48 escape color codes & UTF-8.
    var $STRING_CONSOLE     = (1 << 4);
    # 16: Changes player names only.
    var $STRING_NAMES       = (1 << 5);
    # 32: Changes host names only.
    var $STRING_HOSTS       = (1 << 6);
    function SDK($fString = SELF::STRING_HTML & SELF::STRING_NAMES & SELF_HOST)
    {
        // constructor code.
    }
}

$SDK &= new SDK(SDK::STRING_NONE);

(1 << 0) 对我来说似乎是非常基本的语法,并且无法理解为什么 PHP 不允许这样的事情.任何人都可以想出一种可以保持以下代码的可读性和未来可扩展性的解决方法吗?

(1 << 0) seems like very basic syntax to me, and is not fathomable why PHP would not allow for such a thing. Can anyone think of a work around that would maintain readability and future expandability of the following code?

推荐答案

在 PHP 中声明类常量或属性时,您只能为默认值指定原始值.因此,例如,此类声明将不起作用:

When declaring a class constant or property in PHP you can only specify a primitive values for default values. So for instance, this class declaration won't work:

class TEST {
 const ABC = 2 * 4;
 const DEF = some_function();
 static $GHI = array(
   'key'=> 5 * 3,
 );
}

但是这个类声明将:

class TEST {
 const ABC = 8;
 static $GHI = 15;
}

这些规则适用于类常量/属性的默认值 - 您始终可以使用表达式的结果初始化其他变量:

These rules apply to default values for class constants/properties - you can always initialize other variables with the results of an expression:

$a= array(
 'a'=> 1 * 2,
 'b'=> 2 * 2,
 'c'=> 3 * 2,
);

这个类声明行为的原因如下:表达式就像动词.他们做某事.类就像名词:它们声明某些东西.声明性语句永远不会产生动作语句的副作用.要求原始默认值强制执行此规则.

The reason for this class declaration behavior is as follows: expressions are like verbs. They do something. Classes are like nouns: they declare something. A declarative statement should never produce the side-effects of an action statement. Requiring primitive default values enforces this rule.

考虑到这一点,我们可以如下重构原始类:

With this in mind we can refactor the original class as follows:

class SDK
{

    static protected $_types= null;

    static public function getType($type_name) {
        self::_init_types();
        if (array_key_exists($type_name, self::$_types)) {
            return self::$_types[$type_name];
        } else {
            throw new Exception("unknown type $type_name");
        }
    }

    static protected function _init_types() {
        if (!is_array(self::$_types)) {
            self::$_types= array(
                'STRING_NONE'=> 1 << 0,
                // ... rest of the "constants" here
                'STRING_HOSTS'=> 1 << 6
            );
        }
    }

    function __construct($fString = null) {
        if (is_null($fString)) {
            $fString= self::getType('STRING_NONE') & self::getType('STRING_HOSTS');
        }
        var_dump($fString);
    }

}

$SDK &= new SDK(SDK::getType('STRING_HOSTS')); 

这篇关于未解析基本语法的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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