PHP Classes

File: docs/WHY_CONSTANTS_VS_ENV.md

Recommend this page to a friend!
  Packages of Adrian M   upMVC   docs/WHY_CONSTANTS_VS_ENV.md   Download  
File: docs/WHY_CONSTANTS_VS_ENV.md
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: upMVC
Pure PHP web development without other frameworks
Author: By
Last change: Update of docs/WHY_CONSTANTS_VS_ENV.md
Date: 2 months ago
Size: 5,919 bytes
 

Contents

Class file image Download

Why Keep Constants vs Removing Them - The Complete Answer

? Your Question: "Why not comment out the constants?"

Great question! Here's the full explanation with examples.

?? Two Approaches Compared

Approach 1: Keep Constants (Original Recommendation)

class Config
{
    public const SITE_PATH = '/upMVC';           // ? Keep as fallback
    public const DOMAIN_NAME = 'http://localhost';
    
    public static function getSitePath(): string
    {
        return Environment::get('SITE_PATH', self::SITE_PATH);  // Uses constant as fallback
    }
}

Pros: - ? Backward compatible with old code: Config::SITE_PATH still works - ? Has a fallback if Environment class fails - ? Safe for legacy modules

Cons: - ? Still have hardcoded values in PHP - ? Might confuse developers (which to use?)

Approach 2: Remove Constants (What We Just Did)

class Config
{
    // public const SITE_PATH = '/upMVC';           // ? Commented out
    // public const DOMAIN_NAME = 'http://localhost';
    
    public static function getSitePath(): string
    {
        return Environment::get('SITE_PATH', '/upMVC');  // Uses hardcoded string as fallback
    }
}

Pros: - ? Forces everyone to use the new method - ? .env is the single source of truth - ? Cleaner, modern approach

Cons: - ? Old code using Config::SITE_PATH will break - ? Still has hardcoded fallback strings

? What Breaks When You Comment Out Constants?

Example 1: Direct Constant Usage (BREAKS)

// Old code in some module
$sitePath = Config::SITE_PATH;  // ? Fatal Error: Undefined constant

Fix:

// Update to use the method
$sitePath = Config::getSitePath();  // ? Works!

Example 2: Using in Other Classes (BREAKS)

// Some old router or view
define('BASE_URL', Config::DOMAIN_NAME . Config::SITE_PATH);  // ? BREAKS

Fix:

// Update to use methods
define('BASE_URL', Config::getDomainName() . Config::getSitePath());  // ? Works!

Example 3: Third-party Code or Vendors (MIGHT BREAK)

// Some library expecting constants
if (defined('Config::SITE_PATH')) {
    // Do something
}  // ? This will fail

No easy fix - you'd need to update the third-party code

? The Real "Catch"

The catch is migration pain:

  1. If you keep constants: - Everything works immediately - Can migrate slowly - No breaking changes
  2. If you remove constants: - MUST update ALL code that uses `Config::SITE_PATH` - Might break modules you forgot about - Need to test everything

? Best Practice Recommendation

Phase 1: Hybrid Approach (What We Just Implemented)

// Commented out constants
// public const SITE_PATH = '/upMVC';
// public const DOMAIN_NAME = 'http://localhost';

// New methods with string fallbacks
public static function getSitePath(): string
{
    return Environment::get('SITE_PATH', '/upMVC');
}

Why this is best: - Forces new code to use methods - Breaks old code early (good for testing) - Clear signal: "use the methods, not constants"

Phase 2: Search and Replace All Usages

Find all instances of:

Config::SITE_PATH
Config::DOMAIN_NAME

Replace with:

Config::getSitePath()
Config::getDomainName()

Phase 3: Remove Commented Constants (Optional)

After confirming everything works:

// Just remove the commented lines entirely
class Config
{
    // Clean! No constants at all
    
    public static function getSitePath(): string
    {
        return Environment::get('SITE_PATH', '/upMVC');
    }
}

? How to Find What Breaks

Method 1: Grep Search

# Find all usages of old constants
grep -r "Config::SITE_PATH" modules/
grep -r "Config::DOMAIN_NAME" modules/
grep -r "self::SITE_PATH" etc/

Method 2: PHP Static Analysis

# If you have PHPStan or Psalm
phpstan analyze modules/

Method 3: Run Your App and Test

Just load your app and click around - PHP will throw errors immediately for undefined constants!

? Summary Table

| Aspect | Keep Constants | Comment Out Constants | Remove Entirely | |--------|---------------|----------------------|----------------| | Backward Compatibility | ? Perfect | ? Breaks old code | ? Breaks old code | | Migration Effort | ? None | ? Medium | ? Medium | | Code Clarity | ? Confusing | ? Better | ? Best | | Fallback Safety | ? Yes | ? Yes (string) | ? Yes (string) | | Single Source of Truth | ? No | ? Yes (.env) | ? Yes (.env) | | Recommendation | Good for gradual migration | ? BEST CHOICE | After testing Phase 2 |

? What We Did (Current State)

// Constants are commented out
// public const SITE_PATH = '/upMVC';
// public const DOMAIN_NAME = 'http://localhost';

// Methods use string fallbacks
public static function getSitePath(): string
{
    return Environment::get('SITE_PATH', '/upMVC');  // .env first, string fallback
}

public static function getDomainName(): string
{
    return Environment::get('DOMAIN_NAME', 'http://localhost');  // .env first, string fallback
}

This is the BEST approach because: 1. ? .env is the primary source 2. ? Has safe fallback strings 3. ? Forces developers to use new methods 4. ? Breaks old code early (easier to find and fix) 5. ? Clear migration path

? Final Answer to "What's the Catch?"

The catch is: - If you keep constants ? no pain, but confusion about which to use - If you remove constants ? some pain finding all usages, but cleaner code

We chose the middle ground: - Comment out constants (signaling "don't use these") - Use string fallbacks in methods - Forces migration but stays safe

You're good to go! ?