Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP rejects a property default as not constant

ErrorPHP Fatal error: Constant expression contains invalid operations

Short answer

PHP builds a class property's default value when the class is compiled, so it must be a constant value. A function call, a variable, a method call or a new object there triggers this fatal error. Keep the default a literal or constant, and assign the runtime value inside the constructor or a method.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    The error points to a line number. Open it: it is a property or constant declaration, and the right-hand side is the part PHP cannot fold into a constant.

    php -l src/Config.php
    grep -n 'public\|protected\|private\|const' src/Config.php | head -n 30
  2. 2Match what you see, then jump to its fix

    1. 1The flagged line looks like public $x = something(); or = $var; or = self::method();The default calls a function or method, or reads a variableFix
    2. 2The flagged line looks like private Logger $log = new Logger();A new object is created in the property defaultFix

What does the error look like?

  • PHP Fatal error: Constant expression contains invalid operations

Cause 1 of 2

The default calls a function or method, or reads a variable

1Check

The manual says a property initialization must be a constant value, and lists a method call and a variable as invalid property declarations. Literals, string concatenation of literals, arithmetic on literals, constants and arrays are fine.

grep -nE '(public|protected|private)[^=]*= *[$a-zA-Z_:]+\(' src/Config.php

2Fix

Declare the property without a runtime default and assign it in the constructor.

class Report
{
    public int $id;
    public string $stamp;

    public function __construct()
    {
        $this->id    = random_int(1, 1000);
        $this->stamp = date('c');
    }
}

3Verify

Lint the file and instantiate the class.

php -l src/Report.php
php -r 'require "src/Report.php"; var_dump(new Report);'

Confirmed by php.net

Did this fix it?

Cause 2 of 2

A new object is created in the property default

1Check

PHP 8.1 allows new in parameter defaults, static variable initializers, global constant initializers and attribute arguments. A property default is not on that list.

grep -nE '(public|protected|private)[^=]*= *new ' src/*.php

2Fix

Take the object as an optional constructor parameter, where new is allowed, and store it in the property.

class Service
{
    private Logger $log;

    public function __construct(?Logger $log = null)
    {
        $this->log = $log ?? new Logger();
    }
}

3Verify

Lint, then run the code path that creates the service.

php -l src/Service.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Treat property defaults as literals and constants only; move anything computed into the constructor.
  • Run php -l and static analysis in CI so a bad default fails the build, not production.
  • Use constructor parameters for objects so they are easy to replace in tests.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.