Skip to content

fix: plain PHP gets a Map or a Set as its PHP array at every crossing - #163

Merged
heyJordanParker merged 12 commits into
masterfrom
fix/collections-to-plain-php
Oct 18, 2026
Merged

heyJordanParker merged 12 commits into
masterfrom
fix/collections-to-plain-php

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

A PHP# Map or Set reached plain PHP as the Sharp\Map or Sharp\Set object wherever no parameter typed array asked for the array. A plain PHP caller that read a result, a property, a constant or an attribute's argument got an object, so array_merge, is_array and an array type hint all failed. Spec section 12 now says plain PHP receives the PHP array at every crossing.

What a PHP# developer can now write

  • A Map or a Set reaches plain PHP as its PHP array at a return, a yielded value, an argument, a property read, a constant and an attribute's argument. A Map is the array of its entries, a backed enum key as its backing value, and a Set is the list of its elements. Each Map and Set inside a List crosses too.
  • A place whose declared type can hold one crosses too: an Any, Any?, type parameter or Iterable result or property.
  • Every reader of a property gets the array: ->, an index read, get_object_vars, get_mangled_object_vars, get_class_vars, an (array) cast, foreach, array_walk and ArrayIterator over the object, ReflectionProperty::getValue, and ReflectionClass::getStaticPropertyValue and getStaticProperties. empty() tests the array. serialize, var_export and var_dump keep the object.
  • Reflection types each such property, parameter and result array.
  • A plain PHP class can implement or override a PHP# method that returns a Map or a Set, with array, since such a result lowers to \Sharp\Map|array, as a parameter does.
  • PHP# reading its own property still gets the Map, and a PHP array plain PHP writes there becomes a Map again. A plain PHP generator held as Iterable<Map<string, int>> gives PHP# each element as the Map.
  • A plain PHP parameter that takes only objects, as \Countable, \Traversable, object or an interface, takes the Map or the Set itself. One that names array, iterable or mixed, or declares no type, takes the array.
  • A call through a function value gives the array to a plain PHP closure and the object to a PHP# closure. The bridge cannot see which closure a value holds, so \Sharp\Internal\arrayOf reads the closure at run time.
  • A result that erases to mixed, as Any, Any? or an unbounded type parameter, declares no PHP return type, so a plain PHP parent's untyped method fits it.
  • A PHP# class whose plain PHP parent leaves a method untyped, as Eloquent's getMorphClass(), implements a PHP# interface that types it. The class forwards the method with the interface's type.
  • A Map keyed by a class, a struct, a collection or an enum without backing values reaches plain PHP as the Map, since no PHP array holds its keys. Passing one to an array parameter is a compile error.
  • Every other Map crosses as its array, int|string keys included, so a PHP array plain PHP gave, which PHP# types Map<int|string, Any?>, passes back to plain PHP. When two of its keys would be one PHP array key, as 5 and "5", or a backed enum case and its backing value, the crossing throws a ValueError that names both: "This Map holds both 5 and "5", which plain PHP keeps as one key, so it can't become a plain PHP array". It never drops an entry.

Typed class constants

  • public const Map<string, int> LIMITS = ["card": 100];, public const [int, string] PAIR = [7, "seven"]; and public const Iterable<int> STEPS = [1, 2]; compile and load. PHP types them array, array and iterable.
  • A read of a PHP# constant that declares a collection, a tuple or an Iterable has the declared type.
  • A read of a Map or Set constant makes the object once and shares it, kept under the constant's own key, so a later read costs one lookup. Inside a constant expression, as another constant's value, a literal, an attribute argument or a default, the read is the bare constant, its PHP array, and a property's or parameter's crossing makes the object of it.
  • A Function constant stays refused. A PHP# constant's value is a literal, a constant, typeof(Class), a collection literal or operators on them. Widening that list is an open question.

Engine

  • ZEND_ACC_SHARP_CROSSING is a property flag, 1 << 16, set when the property's #[\Sharp\Crossing(plan, readsArray)] says plain PHP reads its array. No fn_flags bit is used: the bridge marks a crossing result with ZEND_SHARP_RESULT_CROSSES in the attr of its result type, and each return and yield of the function emits ZEND_SHARP_RETURN_ARRAY.
  • \Sharp\Crossing takes an optional second argument, readsArray, false for a Map whose keys no PHP array holds, and a null plan for a property that only crosses outward. \Sharp\Internal\keep takes its closure optionally, and gives null for a key it holds nothing under.
  • sharp_table_array checks for colliding keys with the count: an array with fewer entries than the Map throws. The JIT's static property read saves its opline before it crosses, so the exception handler frees the result.
  • The optimizer's inference keeps a plain PHP property or static read counted unless it resolves the property and finds no crossing, so the JIT frees the array each read builds.
  • The lazy Iterable check converts each element it accepts.
  • sharp/mago: the bridge, the checker and reflection decide from the declared key type, through codex's get_php_array_key_type, whether a PHP array holds a Map's keys.

Tests

collection_crossings.phpt runs a row for each crossing, each seen failing first: results, yields, arguments, property reads through each reader, index reads, statics, constants in each constant-expression place, attribute arguments, reflection types, function values, Any, type-parameter and Iterable places, a plain PHP class implementing and overriding a Map result, unbacked keys, colliding keys into an array parameter, an Any result and arrayOf, an int|string Map with no collision, a get_object_vars array passed back to plain PHP, and a timing row that holds a Map constant read to at most five static reads. collection_crossings_preload.phpt runs the plain PHP readers with preloading, colliding static and return reads included. mixed_keys.phpt reads its Map<string|Standing, int> in PHP# and gets the ValueError in plain PHP. The analyzer's missing-super-call row checks that the help's code compiles. The standard library tests read Regex.match, Regex.matchAll, Url.parseQuery and Binary.unpack as arrays again. The PHP# suite passes without opcache, with the file cache, with shared memory, under every optimizer pass and under both JITs. The upstream suite matches its baseline. table_serialize.phpt and its preloaded twin serialize the Map and Set objects again, made with new, and a PHP# helper names the class behind each field.

…h plain PHP as arrays at every crossing

Plain PHP code and the framework read a PHP# Map or Set as a Sharp\Map or
Sharp\Set object at a property read, a return, an argument, a constant and an
attribute argument, so array_merge, ksort and Laravel's array rule refuse it,
and a PHP# interface method returning Any? fits no untyped plain PHP method.

The note records each crossing's state on master 754ab7c, the mechanism
team-lead approved with the answers to its seven questions, and what it costs a
PHP# read of its own property: nothing at run time.

docs/architecture/slices/
└── collections-to-plain-php.md   <- new: the Slice note
Plain PHP code received a PHP# Map or Set as the object wherever no
`array` parameter asked for the array: a result, a property it read, a
constant, an attribute's argument and an argument to an untyped
parameter. So array functions, `is_array` and array type hints failed
on values PHP# handed over. Spec section 12 now says plain PHP receives
the PHP array everywhere: a Map is the array of its entries and a Set
the list of its elements, at any depth inside a List.

- A function whose result holds a Map or a Set carries
  ZEND_ACC_SHARP_CROSSING, and each of its returns emits
  ZEND_SHARP_RETURN_ARRAY, which gives a plain PHP caller the array and
  a PHP# caller the object.
- A property whose type holds one carries the same flag, and a plain
  PHP read gives the array through `->`, `get_object_vars`, `(array)`,
  `foreach` and `ReflectionProperty::getValue`. PHP# reads its own
  property as the Map, and a plain PHP write still crosses back in.
- Reflection types each such property, parameter and result `array`.
- A Map or Set constant is its PHP array, and an attribute's argument
  too.
- A call through a function value hands `\Sharp\Internal\arrayOf` the
  closure, which keeps the object for a closure of PHP#.
- A result that erases to mixed declares no PHP return type, unless it
  overrides a typed plain PHP method.
- A PHP# class forwards a method its PHP# interface types and its plain
  PHP parent leaves untyped, with the interface's type, and the checker
  no longer refuses that class.
- A Map keyed by a class, a struct, a collection or an enum without
  backing values reaches plain PHP as the Map, and so does a Map two of
  whose keys are one PHP array key.

The optimizer, the function JIT and the tracing JIT learn the array a
plain PHP read or call gets, and collection_crossings runs a row for
each crossing, under preloading too.
patch-coverage found lines no test ran: each PHP type a forwarded
method declares, a method two interfaces require, a spread in a
constant's collection literal, and an Any? result that overrides a
typed plain PHP method. forwarded_methods reads the parent's method in
one chain, so a missing one needs no branch of its own.
Plain PHP gets a Map of string keys and a Set as PHP arrays, so the
serialize rows that read PHP# results print the arrays, with each key.
A Map keyed by an enum without backing values, a struct or a class
still reaches plain PHP as the Map. The rows about the Map and Set
objects themselves take a Map keyed by an enum, or make the object
with new.
…s its type

A class constant typed `[int, string]` or `Iterable<int>` got "This type
is not supported yet in PHP#." Each holds a list literal, a value known
before the code runs, so the checker takes both, and PHP types the
constant `array` or `iterable`.

A read of a PHP# constant that declares a collection, a tuple or an
Iterable now has the declared type, where it had its value's own type:
a list literal under `Set<string>` read as a List, and a tuple's read
refused its unpacking. A class constant holds no object, so a read of a
Map or Set constant makes its PHP array the object again, and PHP#'s
Set and Map methods run on it.
…lace

The review of the crossing found plain PHP still meeting a Sharp\Map or
a Sharp\Set, a leak, and constants that stopped their class loading.

- An index read of a crossing property from plain PHP, as
  `$box->payload['a']`, leaked one array per read under both JITs. The
  inference dropped the read's count for any property. It now keeps it
  unless it resolves the property and finds no crossing.
- A Map or Set constant read inside a constant's value, a literal, an
  attribute argument or a default lowers to the bare constant, its PHP
  array. A property's crossing makes the object of a default the
  compiler folded. Elsewhere a constant read makes the object once and
  keeps it under the constant's own key, so a later read is one lookup.
- A generator's yielded values cross as returns do. So do the values of
  an `Any`, `Any?`, type parameter or `Iterable` result or property.
- The bridge decides each crossing from the declared type. A Map whose
  key type unites two of `int`, `string` and backed enums, as
  `int|string` does, stays the Map: the checker refuses it into an
  `array` parameter, and the run-time check on its keys is gone.
- A result's crossing moves from its fn_flags bit to the attr of its
  result type, which leaves that bit free. A Map result lowers to
  `\Sharp\Map|array`, so a plain PHP class can implement or override
  it.
- `empty()`, `get_mangled_object_vars`, `get_class_vars`, `array_walk`
  and `ArrayIterator` over an object, and ReflectionClass's static
  property reads give the array. Reflection reads a property as `array`
  by its crossing flag, not by its file's name.
- A plain PHP generator held as `Iterable<Map<string, int>>` gives PHP#
  each element as the Map.

The serialize rows test the Map and Set objects again, made with new,
and a PHP# helper names the class PHP# holds behind each field.
…unheld nested key

A read of a Set constant through a type parameter makes the object each
time, since its class is known only when the code runs, and a result
of `List<Map<List<int>, int>>` doesn't cross, since its Map's key has no
PHP array form. Each row runs a line no other row ran.
The checker refused any Map keyed by int|string into a plain PHP array
parameter. PHP# types a plain PHP array as Map<int|string, Any?>, so it
refused passing any PHP array back to plain PHP, the missing-super-call
help's own code included, and the standard library's Regex, Url and
Binary results reached plain PHP as Sharp\Map objects.

- Every Map whose keys a PHP array holds crosses as its array, whatever
  its key type. The checker, the bridge and reflection decide that from
  the key type through one codex function, get_php_array_key_type.
- When the array comes out with fewer entries than the Map, two of its
  keys were one PHP array key, as 5 and "5" are, or a backed enum case
  and its backing value. The crossing then throws a ValueError that
  names both keys, and never drops an entry. A Map with no such keys
  pays one count compare.
- The JIT's static property read saves its opline before the crossing,
  so the exception handler frees the result it leaves.
- Regex.match, Regex.matchAll, Url.parseQuery and Binary.unpack give
  plain PHP arrays again, and Repository.locals is Map<int|string, Any?>
  again.
…plain-php

Two files conflicted. arguments.rs keeps both import lists. The
generated sharp_arginfo.h is rebuilt from the merged sharp.stub.php
with build/gen_stub.php.
@heyJordanParker
heyJordanParker merged commit 7ba42a1 into master Oct 18, 2026
24 checks passed
heyJordanParker added a commit that referenced this pull request Oct 18, 2026
#163 gives plain PHP the array form of a PHP# Set or Map field, so the
rows that read the field's class from plain PHP threw or printed "array".
Each row now asks PHP# whether the field holds the object, and expects
the array form where plain PHP reads it.
@heyJordanParker
heyJordanParker deleted the fix/collections-to-plain-php branch November 3, 2026 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant