Skip to content

feat(class): opt-in constructor-less classes with #[php(no_constructor)] - #855

Merged
ptondereau merged 5 commits into
extphprs:mainfrom
ChiragAgg5k:feat/class-without-constructor
Nov 25, 2026
Merged

ptondereau merged 5 commits into
extphprs:mainfrom
ChiragAgg5k:feat/class-without-constructor

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A #[php_class] without a constructor always gets a public __construct that throws You cannot instantiate this class from PHP.. That makes some PHP class shapes impossible to reproduce:

  • A PHP subclass inherits that constructor, so 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 implements Default makes the class declare no __construct, exactly like a PHP class without one:

  • new works on the class and on PHP subclasses (including subclasses of an abstract class), creating an object initialized with Default::default().
  • ReflectionClass::getConstructor() returns null.
  • A subclass's parent::__construct() fails with PHP's own Cannot call constructor error.
  • A Rust exception class (extending \Exception) inherits Exception::__construct, so new NotFoundError('message', 1) works as in PHP.

Requirements and the Rust-parent case:

  • The attribute requires Default; the macro asserts it at compile time.
  • Under a Rust class that declares a constructor (#[php(extends(Base))]), the class keeps the throwing constructor. Inheriting Base's would run it on an object of another Rust type: new Child() ran Base::__construct, side effects included, then failed with Failed to retrieve reference to this object. register detects this case as a parent constructor registered by the module being started.

Subclass objects: create_object passes 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 Default class and was marked breaking; review moved it behind the attribute.) The opt-in is the RegisteredClass::NO_CONSTRUCTOR constant, so registration doesn't build a T to detect Default.

Tests

tests/src/integration/class/no_constructor.php covers:

  • new and reflection on the class and on PHP subclasses;
  • parent::__construct() from a subclass;
  • clone keeping the subclass;
  • an abstract class extended from PHP;
  • a Default class without the attribute, which still throws;
  • a #[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

@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/class-without-constructor (bb62cd3) with main (b7ffba7)

Open in CodSpeed

@coveralls

coveralls commented Nov 17, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 45177997830

Coverage decreased (-0.05%) to 76.777%

Details

  • Coverage decreased (-0.05%) from the base build.
  • Patch coverage: 18 uncovered changes across 2 files (20 of 38 lines covered, 52.63%).
  • 1 coverage regression across 1 file.

Uncovered Changes

File Changed Covered %
src/builders/class.rs 22 7 31.82%
src/types/class_object.rs 3 0 0.0%
Total (3 files) 38 20 52.63%

Coverage Regressions

1 previously-covered line in 1 file lost coverage.

File Lines Losing Coverage Coverage
src/builders/class.rs 1 84.56%

Coverage Stats

Coverage Status
Relevant Lines: 16126
Covered Lines: 12381
Line Coverage: 76.78%
Coverage Strength: 67.15 hits per line

💛 - Coveralls

@ChiragAgg5k ChiragAgg5k changed the title feat(class)!: declare no constructor for a class whose value has a default feat(class): opt-in constructor-less classes with #[php(no_constructor)] Nov 18, 2026
ChiragAgg5k and others added 3 commits November 23, 2026 12:28
…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
ChiragAgg5k force-pushed the feat/class-without-constructor branch from bc168f1 to e25f693 Compare November 23, 2026 12:33
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
ptondereau merged commit 5f6a927 into extphprs:main Nov 25, 2026
86 checks passed
@ext-php-rs-release ext-php-rs-release Bot mentioned this pull request Nov 25, 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