Stupid constant in class declaration issue

One of our live servers is still on php 5.3
and is breaking on the second line with this message
Parse error: syntax error, unexpected '.', expecting ')' in .......
systemAdministrator is a defined constant that i can echo out with no problem before this
any help would be appreciated

$errors = array(
    1 => array('code' => 1, 'mesg' => 'Error connecting to server: Please contact your <a href="mailto:' . systemAdministrator . '">System Administrator</a>'),
    .......,
    .......,
);

Dani AI

Generated

If the array assignment is happening as part of a class property (or static) declaration, the parser on older PHP (5.3) will choke on the concatenation because property/default initializers must be constant values at compile time — you cannot use runtime string operations there. That’s a common migration pitfall and will produce a cryptic “unexpected '.'” or similar message even when the constant itself works at runtime. As hinted, parse errors can also be caused by an earlier syntax problem, but this class-property vs runtime-expression issue is the most likely culprit here. (php.net)

A simple, reliable fix is to stop building that message inside the property declaration and build it at runtime (for example, in __construct() or a static init). Example (different structure than the original post):

class Example {
    protected $errors;

    public function __construct() {
        $this->errors = array(
            1 => array(
                'code' => 1,
                'mesg' => sprintf(
                    'Error connecting to server: Please contact <a href="mailto:%s">System Administrator</a>',
                    constant('systemAdministrator')
                )
            ),
        );
    }
}

Using constant() or defined() in runtime code is safe and keeps the property initializer free of expressions. (php.net)

Other options: if you must keep static-style initialization, provide a literal default (null or an array of literals) and populate it in a static init method, or upgrade to a PHP version that supports constant scalar expressions (PHP 5.6+ adds more support for compile-time expressions). Also run a quick syntax check (php -l file.php) and isolate the file — parser errors sometimes point at the operator but the real problem is earlier in the file (missing semicolon, unclosed paren, BOM, etc.). (php.net)

Checklist: confirm whether $errors is a class property, move dynamic string building into a constructor or init routine, lint the file, and test under PHP 5.3 to reproduce. This will resolve the “unexpected '.'” symptom without changing the constant itself.

Member Avatar for Member #120589

So you've narrowed it down to this constant? If you take out:

' . systemAdministrator . '

you don't get an error. Is that right? I have my doubts. Sounds as if you have an error upstream.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.