Skip to content

Commit 002dfdb

Browse files
committed
[fix] toArray
1.Ensure proper initialization when custom constructors bypass the standard flow, preventing toArray from crashing.
1 parent f31c6ae commit 002dfdb

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## [v3.1.2] - 2025-11-24
4+
5+
### Fixed
6+
7+
- Fixed a bug where Immutable Objects implementing a custom constructor skipped the normal initialization flow, causing toArray() to consistently throw errors. The initialization step is now guaranteed before property traversal, ensuring toArray() works correctly even with custom constructors.
8+
39
## [v3.1.1] - 2025-11-05
410

511
### Fixed

src/ImmutableBase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ abstract class ImmutableBase
4747
{
4848
private int $mode;
4949
private string $namespace = __NAMESPACE__;
50+
private bool $initialized = false;
5051
private static bool $byNamedConstruct = false;
5152
private ReflectionClass $ref;
5253
/** @var ReflectionClass[] $reflectionsCache */
@@ -361,6 +362,9 @@ private static function getReflection(object $obj): ReflectionClass
361362
private function walkProperties(callable $callback): void
362363
{
363364
$properties = [];
365+
if ($this->initialized === false) {
366+
$this->constructInitialize();
367+
}
364368
for ($c = $this->ref; $c && $c->name !== self::class; $c = $c->getParentClass()) {
365369
array_unshift($properties, ...$c->getProperties());
366370
}

0 commit comments

Comments
 (0)