带常量的 PHP 类变量

发布于 2024-09-27 14:21:47 字数 659 浏览 4 评论 0原文

我在常量 (DEPLOYMENT) 行上收到解析错误。为什么现在允许这样做,或者我错过了什么。

解析错误:解析错误,期望出现“')'”

class UploadComponent extends Object {

    private $config = array(
        'accessKey' => 'XXXX',
        'secretKey' => 'XXXX',

        'images' => array(
            'bucket' => DEPLOYMENT.'-files/images',
            'dns' => false
        ),

        'files' => array(
            'bucket' => DEPLOYMENT.'-files/files',
            'dns' => false
        ),

        'assets' => array(
            'bucket' => DEPLOYMENT.'-files/assets',
            'dns' => false
        )
    );
    ....
}

I am getting a parse error on the lines with the constant (DEPLOYMENT). Why is this now allowed, or am I missing something.

Parse error: parse error, expecting `')'' in

class UploadComponent extends Object {

    private $config = array(
        'accessKey' => 'XXXX',
        'secretKey' => 'XXXX',

        'images' => array(
            'bucket' => DEPLOYMENT.'-files/images',
            'dns' => false
        ),

        'files' => array(
            'bucket' => DEPLOYMENT.'-files/files',
            'dns' => false
        ),

        'assets' => array(
            'bucket' => DEPLOYMENT.'-files/assets',
            'dns' => false
        )
    );
    ....
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奶气 2024-10-04 14:21:47

定义类变量时不能使用变量。相反,请在构造函数内初始化数组:

class UploadComponent extends Object {

    private $config;

    function __construct() {
        $this->config = array(
            'accessKey' => 'XXXX',
            'secretKey' => 'XXXX',

            'images' => array(
                'bucket' => DEPLOYMENT.'-files/images',
                'dns' => false
            ),

            'files' => array(
                'bucket' => DEPLOYMENT.'-files/files',
                'dns' => false
            ),

            'assets' => array(
                'bucket' => DEPLOYMENT.'-files/assets',
                'dns' => false
            )
        );
    }
}

You can't use variables when defining class vars. Initialize your array inside the constructor instead:

class UploadComponent extends Object {

    private $config;

    function __construct() {
        $this->config = array(
            'accessKey' => 'XXXX',
            'secretKey' => 'XXXX',

            'images' => array(
                'bucket' => DEPLOYMENT.'-files/images',
                'dns' => false
            ),

            'files' => array(
                'bucket' => DEPLOYMENT.'-files/files',
                'dns' => false
            ),

            'assets' => array(
                'bucket' => DEPLOYMENT.'-files/assets',
                'dns' => false
            )
        );
    }
}
熊抱啵儿 2024-10-04 14:21:47

原因是“常量”可以动态定义。因此,它们的内容只能在运行时知道,而不能在编译时知道。

The reason is that 'constants' can be defined dynamically. Their contents are therefore only known at run-time, and not compile-time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文