Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/changelogs/v4.8.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
4 changes: 4 additions & 0 deletions system/Config/BaseConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions tests/system/Config/BaseConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use Tests\Support\Config\MergeRegistrarA;
use Tests\Support\Config\MergeRegistrarB;
use Tests\Support\Config\TestRegistrar;
use ValueError;

/**
* @internal
Expand Down Expand Up @@ -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');
Expand Down
Loading