Parse error: syntax error, unexpected 'const' (T_CONST), expecting variable (T_VARIABLE) in Laravel project
You are encountering a classic version mismatch issue between your local development environment and your production hosting. The error message Parse error: syntax error, unexpected 'const' (T_CONST), expecting variable (T_VARIABLE) is not a bug in your code logic; it is the PHP interpreter throwing a tantrum because it doesn't recognize the const keyword where you placed it.
In your specific case, the culprit is the line public const TRANSACTION_READ_UNCOMMITTED = TransactionIsolationLevel::READ_UNCOMMITTED;. This syntax for declaring public class constants was introduced in PHP 5.3. However, looking at the discrepancy between your environments reveals the root cause: your local server runs PHP 7.2.0, while Bluehost is serving PHP 7.0.0.
While PHP 7.0 supports public const for class-level constants, there is a nuance regarding visibility modifiers and where they can be used. Wait, actually, public const was fully supported in PHP 5.3. So why does the error occur on Bluehost if both versions support it?
Let's look closer at the specific error: unexpected 'const'. This usually implies that the parser expected a variable name (T_VARIABLE) immediately after the class declaration but found the keyword const instead, or it encountered a context where constants were not allowed.
However, there is a more likely scenario given the "Bluehost" context and older PHP versions often found in shared hosting environments. Sometimes, the error message is slightly misleading, or the file being parsed is cached bytecode from a previous version, or there is a specific interaction with the use statements combined with the constant declaration that triggers a parse error on very old interpreters if the order of operations isn't handled correctly. But wait, public const is standard since 5.3.
Let's re-evaluate the provided code snippet. The code uses public const. If Bluehost is actually running PHP 7.0 (as stated), that version supports public const. Is it possible the hosting provider's environment is misconfigured, or perhaps the file is being parsed by a different PHP version due to a specific directive?
Actually, there is a known quirk with very old versions of PHP (like 5.x) where strict ordering of class elements was enforced differently, but 7.0 is quite modern. The real issue often lies in how the hosting provider manages their PHP versions. Many "shared" hosts default to older versions or have specific configurations that can conflict with newer Doctrine/PHP 7 code, even if they claim to run 7.0.
Checking the Hosting Environment
The first step is to verify what version of PHP is actually executing your code on Bluehost. Sometimes cPanel settings show a default version (e.g., 7.0), but the specific application pool might be running an older patch level or a different minor version that lacks strict compliance with newer standards, or the file is cached as PHP 5 bytecode.
You can confirm the actual runtime by creating a simple info.php file in your project root (or a temporary folder) containing:
<?php phpinfo(); ?>
Then, access this file via your browser on Bluehost. Look for the "PHP Version" at the top of the page. If it shows 7.0.x, check the specific sub-version. If it is anything other than 7.2 or higher, you might be hitting edge cases with Doctrine DBAL versions that were compiled against newer standards.
Switching to PHP 7.4 or 8.x
The most robust fix is to ensure your application runs on a PHP version that fully supports the modern syntax used by Doctrine and Laravel. Both PHP 7.4 and 8.0+ handle public const declarations flawlessly in all contexts.
In cPanel, navigate to Select PHP Version. Look for a dropdown or list of available versions.
- Select PHP 7.4 or preferably PHP 8.1/8.2 if available.
- Set it as the default version for your account or specifically for the application folder (depending on the cPanel interface).
- Save the changes and wait a few minutes for the cache to clear.
Alternative: Modifying the Code (Not Recommended)
If you are forced to stay on an older PHP version for compatibility reasons, you cannot simply remove public or change the syntax arbitrarily without understanding the implications. However, in some legacy environments, moving the constant declaration outside the class or using private static properties might bypass certain parser quirks, but this introduces architectural debt and breaks modern OOP principles.
Doctrine DBAL relies heavily on public constants for transaction isolation levels. Changing these to private static properties would break your application's interface with Doctrine. Therefore, upgrading the PHP runtime is the only viable solution.
Conclusion
The error is a direct result of running code intended for PHP 7.2+ on an environment that might be misconfigured or strictly enforcing older behavior despite reporting 7.0. By switching Bluehost's execution environment to PHP 7.4 or higher, you align your runtime with the syntax standards required by Doctrine and Laravel, resolving the parse error immediately.