1

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
        )
    );
    ....
}
Lizard
  • 43,732
  • 39
  • 106
  • 167
  • *(sidenote)* You dont want to have the dependency on the global constant in there anyway. – Gordon Oct 14 '10 at 11:21
  • where are you defining "DEPLOYMENT"? – pleasedontbelong Oct 14 '10 at 11:21
  • @Gordon why not? @pleasedontbelong really? What is the point of your comment. – Lizard Oct 14 '10 at 11:55
  • because dependencies ought to be injected. If you rely on outside state, your class can no longer be used in isolation. You cannot have this class without also having the global constant. That's bad for maintenance and testing. – Gordon Oct 14 '10 at 12:24

2 Answers2

7

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
            )
        );
    }
}
Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
5

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

Evert
  • 93,428
  • 18
  • 118
  • 189