What is the best way to define a global constant in PHP which is available in all files?

What is the best way to define a global constant in PHP which is available in all files?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Defining Global Constants in PHP: Syntax, Semantics, and Best Practices

When you need a value that is accessible from every file in your application without passing it through dependency injection or global variables, constants are the standard tool. In PHP, there are two primary mechanisms for defining such values: the define() function and the const declaration. While they often yield similar results at runtime, their underlying behavior, scoping rules, and usage patterns differ significantly.

Using const for Class Constants (The Modern Standard)

In modern PHP development, particularly within frameworks like Laravel, the preferred approach is to define constants inside classes using the const keyword. This syntax creates a static property that acts as a constant.

<?php
class AppConfig {
    const APP_NAME = 'MyApplication';
    const VERSION = '1.0.0';
}

echo AppConfig::APP_NAME; // Outputs: MyApplication

This approach offers several distinct advantages over legacy methods. First, constants defined with const are strictly typed in terms of scope; they belong to the class that defines them. You must access them via the class name or an instance, which prevents accidental collision with namespaced functions or other global entities. Second, const is a compile-time declaration. The parser checks for typos and missing braces immediately during development, failing fast if you misspell APP_NAME.

Because constants are static properties of a class, they are effectively "global" within the context of that namespace. If your application uses strict namespaces (which is best practice), you can treat these values as global resources by referencing them through their fully qualified names or via an autoloaded configuration class.

Using define() for File-Level Global Constants

The alternative is the define() function, which creates a true global constant in the current symbol table. This was the only way to create constants in PHP versions prior to 5.6, but it remains widely used.

<?php
define('MAX_UPLOAD_SIZE', 10 * 1024 * 1024); // 10MB
define('DEBUG_MODE', true);

// Accessible anywhere without a namespace prefix (if in global scope)
echo MAX_UPLOAD_SIZE; 

The critical distinction here is that define() creates a symbol that exists in the global namespace of the current file. Once defined, it cannot be redefined with a different value in another file. If you attempt to call define('MAX_UPLOAD_SIZE', 5MB) in a second file after the first definition, PHP will throw a fatal error regarding redefinition. This immutability is actually a feature; it ensures that core configuration values remain consistent across the entire request lifecycle.

Interchangeability and Trade-offs

Can you use them interchangeably? Not really. While both provide read-only values, the scope dictates their utility. const constants are tied to classes, making them ideal for namespaced applications where global pollution is a risk. They support late static binding in specific contexts and integrate seamlessly with modern PHP type hinting systems when used as return types or property declarations.

define() constants, on the other hand, exist in the global namespace. In large applications, relying on define() can lead to naming conflicts if multiple libraries define a constant with the same name (e.g., USER_AGENT). If you are using Composer and third-party packages, they often rely on specific global constant names, which can clash with your own definitions if not carefully managed.

When to Use Which?

If you are building an application today, specifically one that leverages autoloading and namespaces (like a Laravel project), defining constants inside a dedicated configuration class is the robust choice. It keeps your global namespace clean and allows you to refactor namespacing without breaking existing code.

However, if you need a constant to be available in every single file immediately upon loading, without requiring a specific class prefix, define() remains valid. Just remember to guard against redefinition errors by checking if the constant already exists before defining it, or accepting that the first definition wins and subsequent calls will fail.

For projects using Laravel specifically, you might encounter patterns where configuration is centralized in service providers or dedicated config files rather than scattered global constants. This aligns with the framework's philosophy of keeping the global scope minimal. If you need to access these values throughout your codebase, injecting a configuration interface or accessing the config() helper function is often more aligned with Laravel's architecture than defining raw global constants.

Ultimately, the choice depends on your namespace strategy. If you embrace strict namespacing, const inside classes is safer. If you operate in a flat namespace for legacy reasons, define() provides the necessary global scope but requires vigilance against naming collisions.

Our network