Skip to content

feat(class): declare typed properties with ClassBuilder::typed_property - #856

Merged
ptondereau merged 3 commits into
extphprs:mainfrom
ChiragAgg5k:feat/typed-properties
Nov 18, 2026
Merged

ptondereau merged 3 commits into
extphprs:mainfrom
ChiragAgg5k:feat/typed-properties

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Problem

ClassBuilder::property declares every property without a type; ClassProperty::ty only shapes the generated stubs. Porting a PHP class that declares protected int $count = 0; and keeping its observable behaviour means calling zend_declare_typed_property through a private extern block. That behaviour includes type enforcement on writes, reflection's getType(), the uninitialized state, and PHP subclasses reading the property.

Change

ClassBuilder::typed_property(ClassProperty) declares the property with ty (and nullable) as its PHP type:

  • Types: scalar types, mixed, and classes (DataType::Object with a class name). A property type names its class with an interned zend_string and _ZEND_TYPE_NAME_BIT, rather than the C string argument info uses.
  • Behaviour: the engine enforces the type on every write, coercing in weak mode, and ReflectionProperty::getType() reports it.
  • No default: the property starts uninitialized (IS_UNDEF), exactly like a PHP typed property without a default.
  • Invalid types: a type a property cannot have (void, callable, …), or a missing ty, fails ClassBuilder::register with the new Error::InvalidPropertyType. Error is #[non_exhaustive].
  • Bindings: zend_declare_typed_property joins allowed_bindings.rs, and docsrs_bindings.rs was regenerated with tools/update_bindings.sh.

ClassBuilder::property and the #[php(prop)] fields are unchanged. The flag is kept beside properties inside the builder, so ClassProperty keeps its fields and struct literals still compile.

Motivation: Appwrite is porting Utopia PHP libraries to Rust as classes of a PHP extension. Those classes keep their state in typed protected properties, which PHP subclasses in application code read and write.

Tests

tests/src/integration/class/typed_properties.php, through a #[php(modifier)] class, covers:

  • reflection types and defaults (int, ?string, ArrayObject, float);
  • the uninitialized state and unset();
  • TypeError on writes, and weak-mode coercion;
  • a PHP subclass using the protected property.

The guide's #[php_class] page has a "Typed properties" section, mirrored in the macro docs.

🤖 Generated with Claude Code

…rty`

`ClassBuilder::property` declares every property without a type: its `ty`
only shapes the generated stubs. A class ported from PHP that declares
`protected int $count = 0;` could not be reproduced without calling
`zend_declare_typed_property` through its own extern block.

`typed_property` declares the property with `ty` (and `nullable`) as its
PHP type: scalar types, `mixed`, and classes. The engine enforces the type
on every write and coerces in weak mode, reflection reports it, and without
a default the property starts uninitialized, as in PHP. A type a property
cannot have fails `build` with `Error::InvalidPropertyType`.
`zend_declare_typed_property` joins the allowed bindings.

`property` is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codspeed

codspeed Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 51 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 51 untouched benchmarks


Comparing ChiragAgg5k:feat/typed-properties (96f8121) with main (a2c29cb)

Open in CodSpeed

@coveralls

coveralls commented Nov 17, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 44322820464

Coverage increased (+0.08%) to 76.634%

Details

  • Coverage increased (+0.08%) from the base build.
  • Patch coverage: 41 uncovered changes across 1 file (192 of 233 lines covered, 82.4%).
  • 1 coverage regression across 1 file.

Uncovered Changes

File Changed Covered %
src/builders/class.rs 196 155 79.08%
Total (3 files) 233 192 82.4%

Coverage Regressions

1 previously-covered line in 1 file lost coverage.

File Lines Losing Coverage Coverage
crates/introspection/src/stub.rs 1 87.37%

Coverage Stats

Coverage Status
Relevant Lines: 16019
Covered Lines: 12276
Line Coverage: 76.63%
Coverage Strength: 66.8 hits per line

💛 - Coveralls

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@ptondereau ptondereau left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Here are some feedbacks

Comment thread src/builders/class.rs Resolved
Comment thread src/builders/class.rs Resolved
Review of `typed_property`:

- The type is checked against an allowlist (`property_type_mask`): `bool`,
  `int`, `float`, `string`, `array`, `object`, `mixed` and classes, built on
  a shared `ZendType::scalar_type_mask`. The denylist let through
  `iterable` (arrays refused, the TypeError segfaulting), `resource` and
  constant expressions (segfaulting `ReflectionType::__toString`) and
  `Indirect` (declared as `callable`). `iterable` is refused, since a
  property cannot use arginfo's `_ZEND_TYPE_ITERABLE_BIT`.
- The default is checked as `zend_is_valid_default_value` does: a value of
  the type, or an `int` on a `float` property, which becomes a float; any
  other default is refused with `InvalidPropertyType`.
- `readonly: true`, and a typed property on a readonly class (which the
  engine would declare writable), are refused rather than ignored.
- Typed properties live in their own list rather than a flag beside each
  property; the duplicate check and stubs cover both lists.

`validate()` runs these checks before anything is registered, and unit
tests cover every `DataType` variant and each refusal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ChiragAgg5k

Copy link
Copy Markdown
Contributor Author

Besides the two threads, 96f812197 also:

  • keeps typed properties in their own list instead of a flag beside each untyped one; the duplicate-name check and stub generation cover both;
  • refuses readonly: true on a typed property instead of ignoring it;
  • refuses typed properties on a readonly class (8.2+). zend_declare_typed_property doesn't add ZEND_ACC_READONLY for ZEND_ACC_READONLY_CLASS, so they would have been writable.

These all fail register with InvalidPropertyType, checked in validate() and unit-tested. The guide section lists the limits. #857 now builds its union members on the same scalar_type_mask.

@ptondereau
ptondereau enabled auto-merge (squash) November 18, 2026 16:42
@ptondereau
ptondereau disabled auto-merge November 18, 2026 16:59
@ptondereau
ptondereau enabled auto-merge (squash) November 18, 2026 17:00
@ptondereau
ptondereau disabled auto-merge November 18, 2026 17:00
@ptondereau
ptondereau merged commit 174ef3c into extphprs:main Nov 18, 2026
163 of 165 checks passed
@ext-php-rs-release ext-php-rs-release Bot mentioned this pull request Nov 18, 2026
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.

3 participants