Repository navigation
feat(class): declare typed properties with ClassBuilder::typed_property - #856
Merged
Merged
Conversation
…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>
Merging this PR will not alter performance1 warning
Comparing |
Coverage Report for CI Build 44322820464Coverage increased (+0.08%) to 76.634%Details
Uncovered Changes
Coverage Regressions1 previously-covered line in 1 file lost coverage.
Coverage Stats
💛 - Coveralls |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ptondereau
requested changes
Nov 18, 2026
ptondereau
left a comment
Member
There was a problem hiding this comment.
Thanks for the PR! Here are some feedbacks
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>
Contributor
Author
|
Besides the two threads,
These all fail |
ptondereau
approved these changes
Nov 18, 2026
ptondereau
enabled auto-merge (squash)
November 18, 2026 16:42
ptondereau
disabled auto-merge
November 18, 2026 16:59
ptondereau
enabled auto-merge (squash)
November 18, 2026 17:00
ptondereau
disabled auto-merge
November 18, 2026 17:00
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ClassBuilder::propertydeclares every property without a type;ClassProperty::tyonly shapes the generated stubs. Porting a PHP class that declaresprotected int $count = 0;and keeping its observable behaviour means callingzend_declare_typed_propertythrough a private extern block. That behaviour includes type enforcement on writes, reflection'sgetType(), the uninitialized state, and PHP subclasses reading the property.Change
ClassBuilder::typed_property(ClassProperty)declares the property withty(andnullable) as its PHP type:mixed, and classes (DataType::Objectwith a class name). A property type names its class with an internedzend_stringand_ZEND_TYPE_NAME_BIT, rather than the C string argument info uses.ReflectionProperty::getType()reports it.IS_UNDEF), exactly like a PHP typed property without a default.void,callable, …), or a missingty, failsClassBuilder::registerwith the newError::InvalidPropertyType.Erroris#[non_exhaustive].zend_declare_typed_propertyjoinsallowed_bindings.rs, anddocsrs_bindings.rswas regenerated withtools/update_bindings.sh.ClassBuilder::propertyand the#[php(prop)]fields are unchanged. The flag is kept besidepropertiesinside the builder, soClassPropertykeeps 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:int,?string,ArrayObject,float);unset();TypeErroron writes, and weak-mode coercion;The guide's
#[php_class]page has a "Typed properties" section, mirrored in the macro docs.🤖 Generated with Claude Code