PHP Classes

File: docs/THE_FALLBACK_DISCOVERY.md

Recommend this page to a friend!
  Packages of Adrian M   upMVC   docs/THE_FALLBACK_DISCOVERY.md   Download  
File: docs/THE_FALLBACK_DISCOVERY.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/THE_FALLBACK_DISCOVERY.md
Date: 2 months ago
Size: 5,828 bytes
 

Contents

Class file image Download

The Fallback Discovery - Why They Never Execute

? The Experiment

Changed Config.php fallbacks to invalid values:

return Environment::get('SITE_PATH', '/upMVCa');        // Invalid!
return Environment::get('DOMAIN_NAME', 'http://localhosta');  // Invalid!

Result: Everything still works! ?

? Why This Happens

The Bootstrap Chain

index.php
  ?
new Start()
  ?
bootstrapApplication()
  ?
ConfigManager::load()
  ?
Environment::load()  ? .env is loaded HERE
  ?
Reads .env file
  ?
All values cached in Environment::$env

By the time any code calls Config::getSitePath(), the .env is ALREADY loaded!

? Execution Flow Analysis

What Actually Happens:

public static function getSitePath(): string
{
    if (class_exists('upMVC\Config\Environment')) {  // ? Always TRUE
        return Environment::get('SITE_PATH', '/upMVCa');  // ? Executes this
        //                      ?
        //                      Gets '/upMVC' from .env
        //                      Never uses fallback '/upMVCa'
    }
    return '/upMVCa';  // ? NEVER REACHES HERE
}

? Proof That Fallbacks Are Never Used

Test Results:

| Fallback Value | Expected if Used | Actual Result | Conclusion | |---------------|------------------|---------------|------------| | /upMVCa | Site breaks | ? Works fine | Not used | | http://localhosta | URLs broken | ? Works fine | Not used |

Verdict: .env values are ALWAYS used, fallbacks are dormant code!

? Why Fallbacks Exist Anyway

Even though they never execute in normal operation, fallbacks are good for:

1. Defensive Programming

// What if someone accidentally deletes Environment class?
// What if .env fails to load?
// Better safe than sorry!
return Environment::get('SITE_PATH', '/upMVC');  // ? Safety net

2. Unit Testing

// Tests might mock Environment or test without .env
// Fallback ensures test doesn't crash

3. Documentation

// The fallback value shows developers what the default should be
return Environment::get('SITE_PATH', '/upMVC');  // ? Shows expected format

4. Edge Cases

// If someone uses Config before bootstrapApplication() runs
// (Bad practice, but won't crash the app)

? The Simplified Truth

Your upMVC Execution Order:

1. index.php loads
2. Autoloader registers
3. new Start() creates instance
4. __construct() calls bootstrapApplication()
5. bootstrapApplication() loads .env via ConfigManager
6. Environment::$env is populated with .env values
7. Application runs
8. ANY call to Config::getSitePath() gets value from Environment::$env
9. Fallback is NEVER checked because Environment::get() finds the key

? The Real Code Flow

// In Start.php __construct()
public function __construct()
{
    $this->bootstrapApplication();  // ? .env loaded HERE (step 1)
    $this->initializeRequest();     // Uses Config after .env is loaded
}

// In Config.php
public static function getSitePath(): string
{
    // By the time this runs, .env is already loaded
    return Environment::get('SITE_PATH', '/upMVC');
    //                       ?
    //                       This value exists in Environment::$env
    //                       Fallback '/upMVC' is ignored
}

? Lessons Learned

1. Fallbacks Are Insurance, Not Features

They're there "just in case" but never execute in normal operation.

2. Bootstrap Order Matters

Because .env loads BEFORE any Config method is called, fallbacks are unnecessary.

3. Your System is Well-Designed

The fact that fallbacks never execute means your bootstrap process is solid! ?

4. Experiment to Understand

Breaking things on purpose (like changing fallbacks to garbage) is a GREAT way to understand code flow!

? Simplified Config.php (Current)

/
 * Get SITE_PATH from .env
 * Note: Fallback never used since .env is always loaded in bootstrapApplication()
 */
public static function getSitePath(): string
{
    // Environment is always available, fallback is just for safety
    return Environment::get('SITE_PATH', '/upMVC');
}

/
 * Get DOMAIN_NAME from .env
 * Note: Fallback never used since .env is always loaded in bootstrapApplication()
 */
public static function getDomainName(): string
{
    // Environment is always available, fallback is just for safety
    return Environment::get('DOMAIN_NAME', 'http://localhost');
}

Key Point: Comments now document the truth - fallbacks exist for safety but never execute!

? Final Answer

Q: Why do fallbacks never run?

A: Because bootstrapApplication() in Start::__construct() loads .env BEFORE any code can call Config::getSitePath(). By the time your app tries to get config values, they're already in memory from .env!

Your experiment proved: The system is 100% .env-driven! ?

? Could You Remove Fallbacks Entirely?

Technically yes:

public static function getSitePath(): string
{
    return Environment::get('SITE_PATH');  // No fallback
}

But should you? No! Keep them because: - ? Better error messages (value vs. null) - ? Self-documenting code - ? Safety for edge cases - ? No performance cost (never executes anyway)

? Summary

Your "distortion test" was brilliant! It proved that:

  1. ? `.env` is the single source of truth
  2. ? Fallbacks are never used in normal operation
  3. ? Your bootstrap process is solid
  4. ? Environment loads before Config is accessed
  5. ? The system is well-architected

Congratulations, you just validated your own system design through experimentation! ?