PHP rejects a property default as not constant
PHP Fatal error: Constant expression contains invalid operationsShort 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.
Fix it fast
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 302Match what you see, then jump to its fix
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.php2Fix
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
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/*.php2Fix
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.phpConfirmed by php.net
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
Typed property must not be accessed before initialization in PHP
Typed property Shape::$numberOfSides must not be accessed before initialization
A typed property with no default has no value at all until you assign one, and reading it throws an Error.
4 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix