Hi everyone,
I've been off for quite a while from internals, so I'm going to
re-introduce myself briefly.
I'm Wendell Adriel, I've been working with PHP since 2009.
Worked over a decade with PHP enterprise applications, and currently I'm
working as a Senior Software Engineer in the Laravel OSS Team. You can
check more about me on my website (link in my email signature).
I would like to propose native type declarations for PHP array keys and
values. The RFC draft is available at:
https://wiki.php.net/rfc/typed_array_declarations
The proposal adds array<TValue> for integer-keyed arrays and array<TKey, TValue> when the key type must be declared explicitly. Plain array
declarations remain unchanged.
The RFC proposes three implementation levels that share the same syntax and
Reflection metadata, but they provide different runtime guarantees:
final class ProductCatalog
{
public array<string, Product> $products = [];
}
$catalog = new ProductCatalog();
$catalog->products = ['featured' => new stdClass()]; // Whole-property
assignment
$catalog->products['featured'] = new stdClass(); // Direct dimension
write
- Level 1 parses the declaration and exposes it through Reflection, but
does not validate elements. Both writes are accepted. - Level 2 validates keys and values when an array crosses a declared
boundary, such as a complete property assignment, argument, return value,
default, or typed class constant. The first write throwsTypeError, but
the direct dimension write is still accepted. - Level 3 includes Level 2 and enforces the constraint for later
property mutations and references. Both writes throwTypeErrorbefore the
invalid value is stored.
I currently recommend Level 3 for typed properties because it preserves the
guarantee the declaration appears to make. There is no implementation yet,
because I first wanted to see which implementation level would be accepted.
I will work on one after incorporating early feedback.
I would appreciate any feedback, especially on the runtime semantics and
the appropriate implementation level.
Thanks in advance!
---
Best Regards,
Wendell Adriel.
Software Engineer & Architect
https://wendelladriel.com https://wendelladriel.com
I would like to propose native type declarations for PHP array keys and
values. The RFC draft is available at:
https://wiki.php.net/rfc/typed_array_declarations
Hello,
Thanks for the RFC. I am also a long time silent follower of the list.
I would like to highlight a possible mistake. I copy the text here:
For by-value boundaries, one parameterized array is a subtype of another
when both its key and value types are subtypes:
array<K1, V1> <: array<K2, V2>
when K1 <: K2 and V1 <: V2
There are some problems here:
- You have limited K to int or string, but none of these types can be
subtyped. As a result K1<:K2 never applies. We can only have K1:=K2. - You also claim that V is covariant. However, this holds true only for
read-only arrays. Write operations of an array use V in contra-variant
positions.
I am afraid that we cannot have sound covariant subtyping of an array,
unless we clearly separate the read interface from the write one.
Lazare INEPOLOGLOU
Architecte Logiciel
On Thu, 16 Jul 2026 at 11:27, Wendell Adriel wendelladriel.ti@gmail.com
wrote:
Hi everyone,
I've been off for quite a while from internals, so I'm going to
re-introduce myself briefly.I'm Wendell Adriel, I've been working with PHP since 2009.
Worked over a decade with PHP enterprise applications, and currently I'm
working as a Senior Software Engineer in the Laravel OSS Team. You can
check more about me on my website (link in my email signature).I would like to propose native type declarations for PHP array keys and
values. The RFC draft is available at:https://wiki.php.net/rfc/typed_array_declarations
The proposal adds
array<TValue>for integer-keyed arrays and
array<TKey, TValue>when the key type must be declared explicitly. Plain
arraydeclarations remain unchanged.The RFC proposes three implementation levels that share the same syntax
and Reflection metadata, but they provide different runtime guarantees:final class ProductCatalog { public array<string, Product> $products = []; } $catalog = new ProductCatalog(); $catalog->products = ['featured' => new stdClass()]; // Whole-property assignment $catalog->products['featured'] = new stdClass(); // Direct dimension write
- Level 1 parses the declaration and exposes it through Reflection,
but does not validate elements. Both writes are accepted.- Level 2 validates keys and values when an array crosses a declared
boundary, such as a complete property assignment, argument, return value,
default, or typed class constant. The first write throwsTypeError, but
the direct dimension write is still accepted.- Level 3 includes Level 2 and enforces the constraint for later
property mutations and references. Both writes throwTypeErrorbefore the
invalid value is stored.I currently recommend Level 3 for typed properties because it preserves
the guarantee the declaration appears to make. There is no implementation
yet, because I first wanted to see which implementation level would be
accepted. I will work on one after incorporating early feedback.I would appreciate any feedback, especially on the runtime semantics and
the appropriate implementation level.Thanks in advance!
---
Best Regards,
Wendell Adriel.
Software Engineer & Architect
https://wendelladriel.com https://wendelladriel.com
There are some problems here:
- You have limited K to int or string, but none of these types can be
subtyped. As a result K1<:K2 never applies. We can only have K1:=K2.- You also claim that V is covariant. However, this holds true only for
read-only arrays. Write operations of an array use V in contra-variant
positions.
I am afraid that we cannot have sound covariant subtyping of an array,
unless we clearly separate the read interface from the write one.
Hey Lazare,
Thank you for the feedback.
I'm going to analyze this and update the RFC document accordingly ASAP.
---
Best Regards,
Wendell Adriel.
Software Engineer & Architect
https://wendelladriel.com https://wendelladriel.com
There are some problems here:
- You have limited K to int or string, but none of these types can be
subtyped. As a result K1<:K2 never applies. We can only have K1:=K2.- You also claim that V is covariant. However, this holds true only for
read-only arrays. Write operations of an array use V in contra-variant
positions.
I am afraid that we cannot have sound covariant subtyping of an array,
unless we clearly separate the read interface from the write one.
Hello there,
Lazare, once again, thanks for the feedback:
I just published a v0.2 of the RFC with changes based on your feedback if
you want to take a look at it.
Also, for other members of Internals, I'd be happy to get your feedback on
this.
I plan to start implementation as soon as we agree on an Implementation
Level I should aim for.
Thanks in advance.
---
Best Regards,
Wendell Adriel.
Software Engineer & Architect
https://wendelladriel.com https://wendelladriel.com
Em qui., 16 de jul. de 2026 às 15:18, Wendell Adriel <
wendelladriel.ti@gmail.com> escreveu:
There are some problems here:
- You have limited K to int or string, but none of these types can be
subtyped. As a result K1<:K2 never applies. We can only have K1:=K2.- You also claim that V is covariant. However, this holds true only for
read-only arrays. Write operations of an array use V in contra-variant
positions.I am afraid that we cannot have sound covariant subtyping of an array,
unless we clearly separate the read interface from the write one.Hey Lazare,
Thank you for the feedback.
I'm going to analyze this and update the RFC document accordingly ASAP.---
Best Regards,
Wendell Adriel.
Software Engineer & Architect
https://wendelladriel.com https://wendelladriel.com
There are some problems here:
- You have limited K to int or string, but none of these types can be subtyped. As a result K1<:K2 never applies. We can only have K1:=K2.
- You also claim that V is covariant. However, this holds true only for read-only arrays. Write operations of an array use V in contra-variant positions.
I am afraid that we cannot have sound covariant subtyping of an array, unless we clearly separate the read interface from the write one.
Hello there,
Lazare, once again, thanks for the feedback:
I just published a v0.2 of the RFC with changes based on your feedback if you want to take a look at it.Also, for other members of Internals, I'd be happy to get your feedback on this.
I plan to start implementation as soon as we agree on an Implementation Level I should aim for.Thanks in advance.
---
Best Regards,
Wendell Adriel.
Software Engineer & Architect**
https://wendelladriel.comEm qui., 16 de jul. de 2026 às 15:18, Wendell Adriel wendelladriel.ti@gmail.com escreveu:
There are some problems here:
- You have limited K to int or string, but none of these types can be subtyped. As a result K1<:K2 never applies. We can only have K1:=K2.
- You also claim that V is covariant. However, this holds true only for read-only arrays. Write operations of an array use V in contra-variant positions.
I am afraid that we cannot have sound covariant subtyping of an array, unless we clearly separate the read interface from the write one.
Hey Lazare,Thank you for the feedback.
I'm going to analyze this and update the RFC document accordingly ASAP.---
Best Regards,
Wendell Adriel.
Software Engineer & Architect**
https://wendelladriel.com
Hi Wendell,
Thanks for updating the RFC.
My largest concern is that this proposal substantially overlaps with the reified generics RFC, which is being held until after the current code freeze in late August or early September at the earliest.
In particular, both proposals need to answer many of the same questions around parameterized types, variance, Reflection, runtime enforcement, inference, and type identity. I think we should avoid committing PHP to a separate set of array-specific rules before the broader generics proposal has been discussed. Otherwise, we risk either constraining the generics design or ending up with two parameterization models that behave differently.
My second concern is the proposed variance model. It is extremely complex for a mutable built-in collection. Collections are normally invariant unless their API clearly separates reading from writing. The generics RFC follows that model: generic parameters are invariant by default, and covariance or contravariance is permitted only where their usage can be proven safe.
Here, an array<Dog> is treated as compatible with array<Animal> only at selected by-value boundaries, based on PHP's copy-on-write separation. It then becomes invariant for writable aliases and requires additional rules for shared references.
That does not appear to establish a normal subtype relationship. It establishes context-dependent boundary compatibility whose soundness depends on engine-level array separation and reference behavior. I think that will be challenging for users to understand and difficult for the engine to enforce consistently.
Making parameterized arrays invariant would be substantially simpler:
array<int, Dog> !== array<int, Animal>
If covariance is desirable, I think it should come from a separate read-only collection or interface rather than from mutable arrays.
My third concern is the one-argument syntax:
array<TValue>
This implicitly means:
array<int, TValue>
That is surprising. It is not a general array of values because string keys are rejected, but it is not a list either because the integer keys need not be contiguous or zero-based. I would expect array<TValue> to constrain only the value type, with the key type remaining int|string. A future list<TValue> could express the more restrictive numeric-keyed form.
Lastly, I am concerned about repeated runtime validation. Since ordinary arrays do not retain a trusted element-type identity, an untyped boundary loses any information established by an earlier check:
function foo(array<Bar> $arr): int
{
$acc = 0;
foreach ($arr as $bar) {
$acc += $bar->count;
}
return $acc;
}
function bar(array $arr): int
{
return foo($arr);
}
$arr = get_super_huge_array();
echo bar($arr);
At the call to foo(), PHP must recursively validate the entire array again, even if the same array was previously checked elsewhere. For large or nested arrays, this makes a type declaration potentially introduce an O(n) or recursive O(n) cost at every typed boundary.
This is another reason I think typed arrays should be considered together with the wider generics design. A broader design may be able to provide type identity, inference, specialized collection types, or another mechanism that avoids repeatedly rediscovering the element type by traversing the value.
I would strongly prefer that this RFC wait until the reified generics RFC has been discussed, or at minimum limit itself to syntax and Reflection experimentation without committing to independent variance and runtime semantics.
— Rob
Hey Rob,
Thanks for the feedback.
My largest concern is that this proposal substantially overlaps with the
reified generics RFC, which is being held until after the current code
freeze in late August or early September at the earliest.
In particular, both proposals need to answer many of the same questions
around parameterized types, variance, Reflection, runtime enforcement,
inference, and type identity. I think we should avoid committing PHP to a
separate set of array-specific rules before the broader generics proposal
has been discussed. Otherwise, we risk either constraining the generics
design or ending up with two parameterization models that behave
differently.
That makes total sense.
For sure, we don't want to create two different models or impact the
generics design.
My second concern is the proposed variance model. It is extremely complex
for a mutable built-in collection. Collections are normally invariant
unless their API clearly separates reading from writing. The generics RFC
follows that model: generic parameters are invariant by default, and
covariance or contravariance is permitted only where their usage can be
proven safe.
Making parameterized arrays invariant would be substantially simpler:
array<int, Dog> !== array<int, Animal>
If covariance is desirable, I think it should come from a separate
read-only collection or interface rather than from mutable arrays.
Yeah, I was divided when writing the RFC if I should include this or not,
but I agree that making them invariant would be much easier/simpler to
approach and I'm totally fine if we want to move in this direction.
My third concern is the one-argument syntax:
array<TValue>
This implicitly means:
array<int, TValue>
That is surprising. It is not a general array of values because string
keys are rejected, but it is not a list either because the integer keys
need not be contiguous or zero-based. I would expect array<TValue> to
constrain only the value type, with the key type remaining int|string. A
future list<TValue> could express the more restrictive numeric-keyed form.
That makes total sense, I can update the RFC in this direction.
Lastly, I am concerned about repeated runtime validation. Since ordinary
arrays do not retain a trusted element-type identity, an untyped boundary
loses any information established by an earlier check:
PHP must recursively validate the entire array again, even if the same
array was previously checked elsewhere. For large or nested arrays, this
makes a type declaration potentially introduce an O(n) or recursive O(n)
cost at every typed boundary.
This is another reason I think typed arrays should be considered together
with the wider generics design. A broader design may be able to provide
type identity, inference, specialized collection types, or another
mechanism that avoids repeatedly rediscovering the element type by
traversing the value.
Yeah, since I didn't start implementation yet, this was a big concern from
my side regarding performance.
I agree that waiting to see how the generics design will approach this
could be better.
I would strongly prefer that this RFC wait until the reified generics RFC
has been discussed, or at minimum limit itself to syntax and Reflection
experimentation without committing to independent variance and runtime
semantics.
Yeah, I think that we have two paths from here:
- Doing only Implementation Level 1 for this RFC, it's simple enough to be
on PHP 8.7 IMO, and then deferring Level 2 and 3 to a new RFC after the
generics design. - Defer the whole RFC after the generics design and aim for Implementation
Level 2 or 3 (3 as preferred one).
I'm ok with both paths here.
Whatever you think would be better.
---
Best Regards,
Wendell Adriel.
Software Engineer & Architect
https://wendelladriel.com https://wendelladriel.com
Yeah, I think that we have two paths from here:
- Doing only Implementation Level 1 for this RFC, it's simple enough to be
on PHP 8.7 IMO, and then deferring Level 2 and 3 to a new RFC after the
generics design.- Defer the whole RFC after the generics design and aim for Implementation
Level 2 or 3 (3 as preferred one).
Your "level 1" sounds similar in spirit to the "bound-erased generics" RFC which was just declined: https://wiki.php.net/rfc/bound_erased_generic_types
Reserving the syntax without directly enforcing it was not popular, and could actually make it harder to add an enforced version later, because you'd be changing the behaviour of existing code.
I think this is one of those cases where just talking about the abstract design doesn't get us very far without a concrete plan of implementation - there's plenty of support for the feature in theory, but the reason it hasn't happened is that it's really hard to do well within PHP's runtime type model.
Regards,
Rowan Tommins
[IMSoP]
Hey Rowan,
Your "level 1" sounds similar in spirit to the "bound-erased generics" RFC
which was just declined:
https://wiki.php.net/rfc/bound_erased_generic_types
Reserving the syntax without directly enforcing it was not popular, and
could actually make it harder to add an enforced version later, because
you'd be changing the behaviour of existing code.
I think this is one of those cases where just talking about the abstract
design doesn't get us very far without a concrete plan of implementation -
there's plenty of support for the feature in theory, but the reason it
hasn't happened is that it's really hard to do well within PHP's runtime
type model.
I totally get it.
So maybe for now the best would be to defer this RFC until we get the
generics design wrapped up.
I'll be happy to help in any way I can for it, if needed.
Also, if anyone else has any other feedback, questions, or concerns about
the RFC, I'd be happy to discuss about it.
---
Best Regards,
Wendell Adriel.
Software Engineer & Architect
https://wendelladriel.com https://wendelladriel.com
czw., 16 lip 2026 o 11:25 Wendell Adriel wendelladriel.ti@gmail.com
napisał(a):
Hi everyone,
I've been off for quite a while from internals, so I'm going to
re-introduce myself briefly.I'm Wendell Adriel, I've been working with PHP since 2009.
Worked over a decade with PHP enterprise applications, and currently I'm
working as a Senior Software Engineer in the Laravel OSS Team. You can
check more about me on my website (link in my email signature).I would like to propose native type declarations for PHP array keys and
values. The RFC draft is available at:https://wiki.php.net/rfc/typed_array_declarations
The proposal adds
array<TValue>for integer-keyed arrays and
array<TKey, TValue>when the key type must be declared explicitly. Plain
arraydeclarations remain unchanged.The RFC proposes three implementation levels that share the same syntax
and Reflection metadata, but they provide different runtime guarantees:final class ProductCatalog { public array<string, Product> $products = []; } $catalog = new ProductCatalog(); $catalog->products = ['featured' => new stdClass()]; // Whole-property assignment $catalog->products['featured'] = new stdClass(); // Direct dimension write
- Level 1 parses the declaration and exposes it through Reflection,
but does not validate elements. Both writes are accepted.- Level 2 validates keys and values when an array crosses a declared
boundary, such as a complete property assignment, argument, return value,
default, or typed class constant. The first write throwsTypeError, but
the direct dimension write is still accepted.- Level 3 includes Level 2 and enforces the constraint for later
property mutations and references. Both writes throwTypeErrorbefore the
invalid value is stored.I currently recommend Level 3 for typed properties because it preserves
the guarantee the declaration appears to make. There is no implementation
yet, because I first wanted to see which implementation level would be
accepted. I will work on one after incorporating early feedback.I would appreciate any feedback, especially on the runtime semantics and
the appropriate implementation level.Thanks in advance!
---
Best Regards,
Wendell Adriel.
Software Engineer & Architect
https://wendelladriel.com https://wendelladriel.com
Hi Wendell,
I've been implementing a prototype of persistent list-typed properties
(where list is a structural refinement of array, i.e. array_is_list() must
always hold), and during the implementation I ran into what appears to be a
more general issue that may also affect Level 3 typed arrays.
The interesting part is that this is not specific to list. It seems to be a
consequence of trying to maintain a persistent refinement invariant on
mutable IS_ARRAY values.
A simplified example for typed arrays would be:
class C {
public array<string, int> $data = [];
}
parse_str("a=1&b=2", $c->data);
A typical internal implementation pattern is roughly:
- initialize the output parameter as an empty array,
- the empty array satisfies the declared type,
- populate the HashTable in place,
- return.
For nominal types this is fine. For structural refinements (such as list)
or future element-constrained arrays (array<string, int>, array<int, Foo>,
etc.), the invariant can be violated after the initial type check has
already succeeded.
What my prototype uncovered is that there are really two distinct classes
of operations:
Incremental mutations (DIM writes, append, unset, sort variants, etc.),
where the invariant can often be preserved with operation-specific checks.
Opaque out-parameter builders, where an empty array is first assigned and
then populated internally without further type validation.
The second category appears much harder to support in a general way.
This raises a broader architectural question:
Should Level 3 typed arrays rely on function-specific guards for these
APIs, or should internal APIs gradually move towards a model where the
result is built in a temporary zval and only assigned through a single
typed assignment after construction?
That approach would naturally preserve atomicity and would work not only
for list, but also for future typed arrays.
I'm not claiming this makes Level 3 impossible. Quite the opposite - I
think it highlights an implementation issue that any persistent refinement
over mutable arrays will eventually have to solve, regardless of whether
the refinement is list, array<T>, or something else.
I'd be interested to hear whether this has already been considered during
the design of Level 3, or whether this class of internal APIs simply hasn't
been explored yet.
Kind regards,
Michał Marcin Brzuchalski