Skip to content

Commit 71a5d7f

Browse files
Update README: bare-name MANGLED_VARS is also a fast path now
After the direct-write fast path for bare-name keys, the scoped shape and the flat-bare-name shape both resolve each key via a single properties_info hash lookup + direct slot write — one is no longer "fastest". Frame both as first-class options, highlight the flat-bare- name shape as the ideal match for PDO-row-style flat dicts, and drop the stale "PHP & references are preserved" claim now that PRESERVE_REFS is opt-in.
1 parent 5116a1f commit 71a5d7f

1 file changed

Lines changed: 30 additions & 10 deletions

File tree

README.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,22 @@ properties — including private, protected, and readonly ones — without calli
5656
their constructor, faster than Reflection:
5757

5858
```php
59-
// Set private properties via scoped array (fastest path)
59+
// Scoped array — keyed by declaring class
6060
$user = deepclone_hydrate(User::class, [
6161
User::class => ['id' => 42, 'name' => 'Alice'],
6262
AbstractEntity::class => ['createdAt' => new \DateTimeImmutable()],
6363
]);
6464

65-
// Instantiate from class name with mangled keys (same format as (array) cast)
65+
// Flat bare-name array — ideal for hydrating from a flat row
66+
// (e.g. a PDO result), no scope grouping needed
6667
$user = deepclone_hydrate(User::class,
67-
['name' => 'Alice', 'email' => 'alice@example.com'],
68+
['id' => 42, 'name' => 'Alice', 'email' => 'alice@example.com'],
69+
DEEPCLONE_HYDRATE_MANGLED_VARS,
70+
);
71+
72+
// Mangled keys (same format as (array) $obj cast)
73+
$user = deepclone_hydrate(User::class,
74+
['name' => 'Alice', "\0User\0email" => 'alice@example.com'],
6875
DEEPCLONE_HYDRATE_MANGLED_VARS,
6976
);
7077

@@ -86,12 +93,12 @@ function deepclone_hydrate(object|string $object_or_class, array $vars = [], int
8693
the list.
8794

8895
`deepclone_hydrate()` accepts either an object to hydrate in place or a class
89-
name to instantiate without calling its constructor. PHP `&` references in
90-
`$vars` are preserved through the hydrate.
96+
name to instantiate without calling its constructor. By default, PHP `&`
97+
references in `$vars` are dropped on write; pass `DEEPCLONE_HYDRATE_PRESERVE_REFS`
98+
to keep them.
9199

92100
By default, `$vars` is keyed by declaring class name; each value is an array
93-
of property names to values. This is the fastest path — direct slot writes,
94-
no key parsing.
101+
of property names to values:
95102

96103
```php
97104
$user = deepclone_hydrate(User::class, [
@@ -101,17 +108,30 @@ $user = deepclone_hydrate(User::class, [
101108
```
102109

103110
Pass `DEEPCLONE_HYDRATE_MANGLED_VARS` in `$flags` to interpret `$vars` as a
104-
flat mangled-key array (the same shape `(array) $object` produces):
105-
`"\0ClassName\0prop"` for private, `"\0*\0prop"` for protected, bare name
106-
for public/dynamic. Each key is resolved to its scope automatically.
111+
flat key array. Keys can be bare property names (auto-resolved to the
112+
declaring class, the ideal shape for hydrating from a PDO row), or
113+
mangled (`"\0ClassName\0prop"` for private, `"\0*\0prop"` for protected — the
114+
same shape `(array) $object` produces). The two forms can be mixed:
107115

108116
```php
117+
// Bare names — each is resolved to its declaring class automatically
118+
$user = deepclone_hydrate(User::class,
119+
['id' => 42, 'name' => 'Alice', 'email' => 'alice@example.com'],
120+
DEEPCLONE_HYDRATE_MANGLED_VARS,
121+
);
122+
123+
// Mangled keys, typical (array) cast shape
109124
$user = deepclone_hydrate(User::class,
110125
['name' => 'Alice', "\0User\0email" => 'alice@example.com'],
111126
DEEPCLONE_HYDRATE_MANGLED_VARS,
112127
);
113128
```
114129

130+
Both the scoped shape and the flat-bare-name shape take a direct
131+
`properties_info` lookup per key followed by a direct slot write — there's
132+
no meaningful performance difference between the two, pick whichever
133+
representation your caller already has on hand.
134+
115135
`$flags` selects the write semantics for declared-property assignments:
116136

117137
| Flag | Semantics |

0 commit comments

Comments
 (0)