Static Constants Are Not First-Class Citizens in PHP 7 and Earlier
Attempting to define a public static const property directly inside a class definition often results in a baffling syntax error for developers transitioning from other languages or those expecting modern JavaScript-like behavior. Consider the following code snippet:
class Patterns
{
public static const EMAIL = "/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix";
public static const INT = "/^\d+$/";
public static const USERNAME = "/^\w+$/";
}
When you run this script, PHP throws a syntax error, unexpected T_CONST, expecting T_VARIABLE. This happens because the syntax for defining constants differs significantly from how properties are defined, and the ability to declare them statically at the class level has specific constraints depending on your PHP version.
In PHP versions prior to 8.0, constants could only be declared using the const keyword outside of a class block entirely, or within a class but without the public static modifier. The language parser in these versions did not recognize the combination of static and const inside a class definition as valid syntax. To achieve the same goal in PHP 7 or earlier, you would have to define the constants globally before the class exists:
define('EMAIL', "/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix");
define('INT', "/^\d+$/");
class Patterns {
// You can reference the global constants here if needed,
// but you cannot define new ones inside the class with 'static const'.
}
While this approach works, it lacks encapsulation. Global namespace pollution is a well-known anti-pattern that makes code harder to maintain and debug. The real solution arrived with PHP 8.0, which introduced native support for defining static constants directly within classes using the public static const syntax. If you are working on a legacy project running PHP 7.x, you must choose between using the define() function (which is limited to global scope), creating properties with public static readonly (though this is technically a property with a constant-like value), or upgrading your runtime environment.
It is crucial to distinguish between const and readonly. The const keyword was borrowed from languages like Java and C++ but implemented differently in PHP, particularly regarding scope. In PHP 8.1 and later, the language introduced readonly properties for class-level constants. While public static const works in PHP 8.0+, public static readonly became the preferred syntax in 8.1+ for defining immutable values within a class namespace.
If you are maintaining code that relies on regex patterns or other unchanging logic, upgrading to PHP 8.0 or higher is often the most pragmatic step. This allows you to keep your validation rules encapsulated within the Patterns class, ensuring they cannot be accidentally overwritten and making them discoverable via static analysis tools.
For modern Laravel applications, leveraging PHP 8 features is standard practice. If your team is still deploying on a PHP 7 stack, consider using a dependency manager or composer configuration to ensure the minimum required version enforces these capabilities. Alternatively, you can define these patterns in a base trait or a dedicated configuration file that adheres to the legacy syntax requirements if an upgrade is not immediately feasible.
The error message unexpected T_CONST is a clear indicator that your parser does not support the modern class-level constant definition. Don't try to hack around this by mixing property and constant definitions; instead, align your runtime version with the language features you wish to use. If you need further guidance on upgrading or configuring your environment for PHP 8 features, resources like https://laravelcompany.com can provide context on how modern PHP versions interact with popular frameworks.