Skip to content

[#15711] Fix toArray() and unserialize() for typed PHP properties that are uninitialized#16905

Merged
niden merged 3 commits intophalcon:5.0.xfrom
niden-code:T15711-apcu-find-findfirst
Apr 23, 2026
Merged

[#15711] Fix toArray() and unserialize() for typed PHP properties that are uninitialized#16905
niden merged 3 commits intophalcon:5.0.xfrom
niden-code:T15711-apcu-find-findfirst

Conversation

@niden
Copy link
Copy Markdown
Member

@niden niden commented Apr 22, 2026

Hello!

In raising this pull request, I confirm the following:

  • I have read and understood the Contributing Guidelines
  • I have checked that another pull request for this purpose does not exist
  • I wrote some tests for this PR
  • I have updated the relevant CHANGELOG
  • I have created a PR for the documentation about this change

Small description of change:

Root Cause

When a query result (e.g. from a LEFT JOIN with no matching row) returns NULL for a column that is NOT NULL in the database schema, cloneResultMap() deliberately skips the property assignment to avoid a TypeError. This leaves any typed non-nullable PHP property (e.g. public int $inv_id) uninitialized.

Two downstream failures then occur:

Bug 1 — toArray() with a getter

If the model defines a getter for the property (e.g. getInvId()), calling toArray() with the default useGetter = true invokes that getter, which reads the uninitialized typed property and throws:

Error: Typed property ... must not be accessed before initialization

This is the error reported in the issue stack trace (occurring during Apcu->set() on the first request).

Bug 2 — unserialize()

Because toArray() returns null for an uninitialized property (via the isset() fallback path), the serialised data stores inv_id => null. On the next request, unserialize() tries to assign that null back to public int $inv_id, throwing:

TypeError: Cannot assign null to property of type int

Fixes

  • Model::toArray(): wrap the getter call in try/catch \Error so that accessing an uninitialized typed property through a getter returns null instead of propagating.
  • Model::unserialize(): wrap each property assignment in try/catch \TypeError so that restoring a null value into a typed non-nullable property is silently skipped rather than crashing.

Thanks

…ze property assignment loop for typed properties
@niden niden added bug A bug report status: medium Medium 5.0 The issues we want to solve in the 5.0 release labels Apr 22, 2026
@niden niden self-assigned this Apr 22, 2026
@niden niden requested a review from Jeckerson April 22, 2026 14:25
@niden niden marked this pull request as ready for review April 22, 2026 14:27
@niden niden merged commit e002b74 into phalcon:5.0.x Apr 23, 2026
3 checks passed
@niden niden deleted the T15711-apcu-find-findfirst branch April 23, 2026 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

5.0 The issues we want to solve in the 5.0 release bug A bug report status: medium Medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants