From 5393703a087085006a4808338cbc41c995596ce4 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Fri, 20 Nov 2026 22:51:21 +0800 Subject: [PATCH] fix: skip env overrides for anonymous `BaseConfig` subclasses on PHP 8.6 --- docs/changelogs/v4.8.0.md | 2 ++ system/Config/BaseConfig.php | 4 ++++ tests/system/Config/BaseConfigTest.php | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/docs/changelogs/v4.8.0.md b/docs/changelogs/v4.8.0.md index f55d4b2a53f9..92a08a293887 100644 --- a/docs/changelogs/v4.8.0.md +++ b/docs/changelogs/v4.8.0.md @@ -393,4 +393,6 @@ Release Date: Unreleased ## Bugs Fixed +- **Config:** Fixed a bug where instantiating an anonymous `BaseConfig` subclass threw a `ValueError` from `getenv()` on PHP 8.6. + See the repo's [CHANGELOG.md](https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md) for a complete list of bugs fixed. diff --git a/system/Config/BaseConfig.php b/system/Config/BaseConfig.php index bfe40e25a292..411bc76c11e4 100644 --- a/system/Config/BaseConfig.php +++ b/system/Config/BaseConfig.php @@ -175,6 +175,10 @@ public function __construct() $this->registerProperties(); + if (str_contains(static::class, "\0")) { + return; // PHP 8.6 getenv() throws for names with null bytes, as in anonymous class names + } + $values = get_object_vars($this); $properties = array_keys($values); $prefix = static::class; diff --git a/tests/system/Config/BaseConfigTest.php b/tests/system/Config/BaseConfigTest.php index bd3754aadd93..2d5d8c6dea02 100644 --- a/tests/system/Config/BaseConfigTest.php +++ b/tests/system/Config/BaseConfigTest.php @@ -40,6 +40,7 @@ use Tests\Support\Config\MergeRegistrarA; use Tests\Support\Config\MergeRegistrarB; use Tests\Support\Config\TestRegistrar; +use ValueError; /** * @internal @@ -137,6 +138,27 @@ public function testBasicValues(): void $this->assertSame(18, $config->golf); } + public function testAnonymousSubclassSkipsEnvironmentOverrides(): void + { + $dotenv = new DotEnv($this->fixturesFolder, '.env'); + $dotenv->load(); + + $named = new SimpleConfig(); + $alpha = null; + + $exception = null; + + try { + $alpha = (new class () extends SimpleConfig {})->alpha; + } catch (ValueError $exception) { + // ... + } + + $this->assertNull($exception, 'BaseConfig should skip environment overrides for anonymous subclasses.'); + $this->assertSame('pow', $named->alpha); + $this->assertSame('one', $alpha); + } + public function testUseDefaultValueTypeIntAndFloatValues(): void { $dotenv = new DotEnv($this->fixturesFolder, '.env');