Repository navigation
feat(class): opt-in constructor-less classes with #[php(no_constructor)] - #855
Merged
ptondereau merged 5 commits intoNov 25, 2026
Merged
Conversation
Merging this PR will not alter performance1 warning
Comparing |
Coverage Report for CI Build 45177997830Coverage decreased (-0.05%) to 76.777%Details
Uncovered Changes
Coverage Regressions1 previously-covered line in 1 file lost coverage.
Coverage Stats
💛 - Coveralls |
#[php(no_constructor)]
…fault A `#[php_class]` without a constructor always got a public `__construct` that throws "You cannot instantiate this class from PHP.". A PHP subclass inherits it, so `new Child()` throws even when the child declares nothing, and `ReflectionClass::getConstructor()` reports a constructor the class never had. When the type implements `Default`, `create_object` already initializes every object from `Default::default()`, so the constructor is not needed: the class now declares none, like a PHP class without one. `new` works on the class and on PHP subclasses, `getConstructor()` is `null`, and a subclass's `parent::__construct()` fails as it does for a PHP parent without a constructor. Types without `Default` keep the throwing constructor, since their objects could not be initialized. `create_object` also passes the engine's class entry on to the default instance, so an object created for a PHP subclass is of the subclass, not of the Rust class. BREAKING CHANGE: a `#[php_class]` that implements `Default` and declares no constructor is now constructable from PHP with `new`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`BaseError` and `NotFoundError` implement `Default` and declare no constructor, so they declare none now and inherit `Exception::__construct`; their stubs no longer list one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nstructor)]` Review of the first version, which dropped the constructor of every `#[php_class]` implementing `Default`: - `default_init` detects `Default` on its own, so a class deriving it for convenience (a handle only ever returned from Rust) would silently become constructable. The class now declares no constructor only with `#[php(no_constructor)]`, which requires `Default` at compile time; every other class keeps today's behaviour, so this is no longer a breaking change, and the hello-world stubs are unchanged. - A `#[php(no_constructor)]` class under a Rust class that declares a constructor would inherit it: `new Child()` ran the parent's constructor and then failed to find a `Parent` in a child object. It keeps the throwing constructor when the parent's constructor belongs to the module being started. - The opt-in is a `RegisteredClass::NO_CONSTRUCTOR` constant, so registration no longer builds a `T` to probe for `Default`. The subclass fix in `create_object` (the object of the class `new` names) is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ChiragAgg5k
force-pushed
the
feat/class-without-constructor
branch
from
November 23, 2026 12:33
bc168f1 to
e25f693
Compare
Nightly rustfmt (2026-11-22) no longer breaks a line inside a markdown link, so tools/update_lib_docs.sh produced a different copy and the macro docs check failed.
…lder::register With typed properties on main, register went over clippy's too_many_lines limit.
ptondereau
approved these changes
Nov 25, 2026
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
A
#[php_class]without a constructor always gets a public__constructthat throwsYou cannot instantiate this class from PHP.. That makes some PHP class shapes impossible to reproduce:new Child()throws even when neither class declares one. This matters when porting a PHP library to Rust whose classes are extended by PHP code, such as an abstract base class with no constructor.ReflectionClass::getConstructor()reports a constructor the PHP original never had.Change
#[php(no_constructor)]on a struct that implementsDefaultmakes the class declare no__construct, exactly like a PHP class without one:newworks on the class and on PHP subclasses (including subclasses of an abstract class), creating an object initialized withDefault::default().ReflectionClass::getConstructor()returnsnull.parent::__construct()fails with PHP's ownCannot call constructorerror.\Exception) inheritsException::__construct, sonew NotFoundError('message', 1)works as in PHP.Requirements and the Rust-parent case:
Default; the macro asserts it at compile time.#[php(extends(Base))]), the class keeps the throwing constructor. InheritingBase's would run it on an object of another Rust type:new Child()ranBase::__construct, side effects included, then failed withFailed to retrieve reference to this object.registerdetects this case as a parent constructor registered by the module being started.Subclass objects:
create_objectpasses the engine's class entry to the default instance (ZendClassObject::new_in), so an object created for a PHP subclass is an object of the subclass rather than of the Rust class.Not breaking: classes without the attribute keep today's behaviour, and the hello-world stubs are unchanged. (The first push of this PR changed every
Defaultclass and was marked breaking; review moved it behind the attribute.) The opt-in is theRegisteredClass::NO_CONSTRUCTORconstant, so registration doesn't build aTto detectDefault.Tests
tests/src/integration/class/no_constructor.phpcovers:newand reflection on the class and on PHP subclasses;parent::__construct()from a subclass;Defaultclass without the attribute, which still throws;#[php(no_constructor)]child of a Rust class with a constructor: it throws, and the parent's constructor does not run.The guide's constructor section and class options, and their macro-doc copies, describe the attribute.
🤖 Generated with Claude Code
Part of: appwrite-labs/cloud#7503