Skip to content

fix: an interface method takes attributes, and a plain PHP class that defines __get has the properties it reads - #159

Merged
heyJordanParker merged 9 commits into
masterfrom
fix/plain-php-members
Oct 18, 2026
Merged

heyJordanParker merged 9 commits into
masterfrom
fix/plain-php-members

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

Forms that failed when part of a real Laravel app moved to PHP#.

What a PHP# developer can now write

  • An attribute on every declaration C# takes one on. PHP# refused one on an interface, an interface method, an interface property, an event and a constant as "not supported yet", and one on a lambda was a parse error. Each now lowers where PHP holds it, and Reflection reads it, with opcache preloading too. A lambda's attributes come before its parameters, as C# 10 writes [Pure] (int n) => n * 2, and lower to PHP's #[Pure] fn (int $n) => $n * 2. An attributed lambda without parentheses is one error: "A lambda with attributes puts its parameters in parentheses, as in [Pure] (n) => n * 2." Spec section 1 lists every place, with checked examples.
  • An attribute argument may new an object from constants, as in [Route("/orders", middleware: [new Throttle(perMinute: 60)])], as PHP and Java's nested annotations take. The class is named, plain PHP or PHP#, and its arguments are constants, typeof, list and map literals or more such news. PHP creates the object each time it reads the attribute. Anything else reads "An attribute argument is a constant or a new of constants, and this.limit() is neither." in place of "not supported yet". A generic class reads "An attribute argument creates its objects without type arguments, so it cannot new a generic class, as new Box<int>(1) does." Spec section 1 states what an argument may be, with a checked example.
  • A named argument fills a plain PHP ... parameter under its name, as PHP collects it: Html.tag("a", href: "/home") and [Generic(Item: typeof(DatabaseEntity))]. It applies to a method, a static method, a constructor, a function and an attribute, only for names that match no other parameter, and the element type checks each value. A PHP# ... parameter is a List, which has no names, and a function built into PHP takes its ... values by position, so both stay compile errors, as in "global.sprintf takes its ...values by position: pass them without names." Spec section 16 states the rule, with a checked example.
  • Plain PHP that passes names to a PHP# ... parameter throws on entry, an override of a plain PHP ... method's included: "Wrapper.wrap takes ...attributes by position, and was given class: and id:." No name or value is lost. Spec section 16 states it.
  • An attribute target names the part of a declaration a bracket goes on, as C# writes it: type:, event: on an event, method: on a method, an operator, a named constructor, a lambda or an accessor, param:, field: and property:, as in [param: Trimmed] set;. A parameter that declares a property, a constructor's, an event's or a data case's, takes param: on the parameter and property: or field: on the property. A target that names no part of its declaration is an error that lists the ones it has.
  • Every accessor form takes attributes, as in { [Searchable] get; [param: Trimmed] set; }, which PHP keeps on the property hook: a property with or without bodies, an interface property, a promoted parameter, a get-only auto-property and a static property. A get-only auto-property with an attributed get; runs as protected(set) with a hook, since PHP's readonly takes no hooks, and a static one's accessors run as hidden methods over the storage. A Map property's index writes and remove work through an attributed accessor. Reflection reads each, with opcache preloading too.
  • What has no PHP place is a compile error in PHP# words. "A return value takes no attributes: put them on the method, with no target." "A type parameter takes no attributes: put them on the declaration that declares it." willSet and didSet run inside set, so they take none. A repeated non-repeatable attribute on a promoted parameter is an error, since an untargeted list goes on both the parameter and the property.
  • subscription.contact reads the Eloquent relation, as $subscription->contact does in PHP. A plain PHP class that defines __get has the properties it reads, and so does a PHP# class whose plain PHP parent defines it, as a PHP# Eloquent model does. A property is typed by an @property or @property-read line on the class or its parents, and is Any? otherwise. A declared property the code can't read, such as a model's protected table or hidden, is the __get property, as plain PHP reads it from outside. As feat: singles — List arrival, never, parameter properties and defaults, toString templates #156 made a declared property win over a method of its name, the read gives the method's value only where the code takes a function the property's type does not fit, and subscription.contact() calls the method. Spec section 14.3 gains the rule, with checked examples that run.
  • A __get property reads what __get gives back, so row.count = "7"; return row.count; into a string return is the usual Any? error, not a TypeError at run time. A write to an undocumented one takes any value and warns nothing.
  • The messages about these properties and attributes speak PHP#. A read typed Any? reads "The type of row.owner is unknown, so the read is Any?." with help that shows as and is. A misplaced attribute's help names the [Attribute(…)] PHP# writes, and a write to an @property-read property says "Remove the assignment: the property is only read." In a plain PHP file each message is unchanged.

The checker, the lowering and the engine

  • The parser reads […] groups before a parenthesized parameter list and => as the lambda's attributes. A list literal is never followed by both, so [a, b] and [twice][0](2) read as before.
  • The analyzer reads the property __get serves before the method, on any class that has __get, and records the method as the read's call target where it takes it, as feat: singles — List arrival, never, parameter properties and defaults, toString templates #156 does for a declared property. The lowering follows the same order.
  • The analyzer maps a named argument that matches no declared parameter of plain PHP code to its ... parameter, in a .sharp file only. The lowering crosses a Map or a Set named there as it crosses one passed by position. The engine's ZEND_RECV_VARIADIC throws for names passed to a function from a .sharp file.
  • The parser keeps an attribute list's target. The analyzer checks each list against the PHP declaration it lands on, so [param: X] beside [X] on set is no repeat. One rule, PropertyHookList::has_hooks, decides which accessors run as PHP hooks, and the scanner, the readonly rule and the lowering read it. The lowering gives set a $value parameter with the property's type when param: names it, and declares a property beside its parameter when the parameter's lists have targets, on a constructor, an event and a data case.

Harness

  • Zend/tests/sharp/harness/Attributes.inc gains Lib\Route and Lib\Throttle, and Zend/tests/sharp/harness/Markup.inc holds the plain PHP ... parameters the engine tests call and a class whose own code passes names to its ... method. sharp/harness is unchanged: the new spec examples declare their plain PHP classes in their own php blocks.

Found, not fixed

  • The function JIT asserts on any PHP# lambda under preloading, at zend_jit.c's !(op_array->fn_flags & ZEND_ACC_CLOSURE) || !(op_array->scope). It gets its own PR.
  • Plain PHP appending to a property a PHP# subclass hooks throws. It belongs to the by-reference crossings PR.

… defines __get has the properties it reads

Two forms failed when part of a real Laravel app moved to PHP#.

An attribute on an interface method was refused as "not supported
yet", though spec section 1 says attributes follow C#'s rules, which
allow one there. The checker now takes an attribute list on an
interface method as on a class method. The lowering already wrote it on
the interface's method and on the default body's trait method, so
reflection reads it, with opcache preloading too.

A PHP# read of an Eloquent relation, subscription.contact, gave the
method contact() as a function value, where plain PHP's
$subscription->contact gives the loaded relation through __get. Moved
into an Any? place, the mistake compiled and ran wrong. Now a plain PHP
class that defines __get has the properties it reads, typed by its
@Property and @property-read lines and Any? otherwise. As #156 made a
declared property win over a method of its name, the read is the
property, and the method's value only where the code takes a function
the property's type does not fit. x.name() still calls the method.

- The analyzer reads the property __get serves before the method, and
  records the method as the read's call target where it takes it, as
  #156 does for a declared property.
- The lowering's read of an object takes the property __get serves
  before the method, unless the analysis recorded the method. A typed
  @property-read read, which the analyzer already typed as the property,
  had lowered as the method's closure.
- The check that a member is one kind on every class a receiver can be
  counts the property __get serves, and a method the analysis recorded,
  so the lowering never meets two kinds.

The spec names interface methods in section 1, and section 14.3 gains
the __get rule. Each gets a checked example.
…ubclass of a __get class has its properties, and the Any? read warning speaks PHP#

An attribute on an interface, an interface property, an event or a
constant was refused as "not supported yet", and one on a lambda was a
parse error. C# allows each, and PHP has a place for each. The checker
takes them, and the lowering writes each list where php-src's grammar
holds it: the 4th child of the interface's or the event's class, the
3rd of the property group, the 2nd of the constant group and the 5th of
the closure or arrow function. Reflection reads every one, with opcache
preloading too. The parser reads `[…]` groups before a parenthesized
parameter list and `=>` as the lambda's attributes, as C# 10 writes
`[Pure] (int n) => n * 2`. A list literal is never followed by both, so
`[a, b]` and `[twice][0](2)` read as before.

A PHP# class whose plain PHP parent defines __get, as a PHP# Eloquent
model does, now has the properties __get reads, as a plain PHP class
does: `row.owner` reads the relation, typed by an @Property line on the
class or its parents and Any? otherwise. Only plain PHP defines __get,
so the analyzer, the kinds check and the lowering ask only whether the
class has it.

The warning on such a read typed Any? named `$owner` and asked for a
PHP docblock tag. In a .sharp file it now reads "The type of
`row.owner` is unknown, so the read is `Any?`.", and its help shows
`string? owner = row.owner as string;` and `if (row.owner is string
owner)`. A read that an `as`, an `is` or a `match` tests no longer
warns, since the test states its type. A plain PHP file's warning is
unchanged.

Spec section 1 lists every place an attribute goes, with checked
examples, and section 14.3 covers the PHP# subclass and the warning,
with a checked example that runs.
An attribute argument may now create an object, as PHP and Java's
nested annotations do:

    [Route("/orders", middleware: [new Throttle(perMinute: 60)])]

PHP creates the object each time it reads the attribute. The class is
named, plain PHP or PHP#, and every argument is a constant, typeof, a
list or map literal, or another such new. The lowering already wrote
the NEW php-src's grammar builds in a constant expression, so Reflection
reads each object, with opcache preloading too.

Anything else in an attribute argument now reads "An attribute argument
is a constant or a `new` of constants, and `this.limit()` is neither."
in place of "This expression is not supported yet in PHP#.": a call, a
named constructor, an object initializer and a spread. A generic
class's object is an error of its own, since PHP creates it without
its type arguments.

Spec section 1 states what an attribute argument may be, with a checked
example.
PHP collects a named argument that matches no other parameter into a
userland function's variadic, and PHP# refused it:

    Html.tag("a", href: "/home", title: "Home");
    [Generic(Item: typeof(DatabaseEntity))]

Now a named argument that matches no declared parameter of a plain PHP
method, static method, constructor, function or attribute fills its
... parameter, and the parameter's element type checks the value, as it
checks a positional one. A Map named there goes in as its PHP array, as
it does by position. A name after positional values in the same ...
parameter, as PHP allows, no longer warns in a .sharp file.

A PHP# ... parameter is a List, which takes no names, and a function
built into PHP throws "does not accept unknown named parameters", so
both stay errors, each with its own reason.

Spec section 16 states the rule, with a checked example, in place of
"A named argument that matches no parameter is a compile error, also on
a method that takes any number of arguments".
…butes

C# names the part of a declaration an attribute list goes on, and PHP#
refused it as "not supported yet". It also refused an attribute on an
accessor, though PHP keeps one on its property hook:

    [property: Field(label: "Total")]
    public int total { [Searchable] get; [param: Trimmed] set; } = 0;

Now a list may open with a target, as C# writes it: type: on a type,
method: on a method, a lambda or an accessor, param: on a parameter and
on the value set or init takes, field: on a field, a constant or an enum
case, property: and field: on a property, and param:, property: and
field: on a constructor parameter that declares a property, which then
declares the property beside the parameter so each takes its own. A
target that names no part of its declaration is an error that lists the
ones it has, and the analyzer checks each list against the PHP
declaration it lands on.

An accessor's attributes go on its hook, an interface property's on its
abstract hook, and a static property's on the hidden method its accessor
runs as. An auto accessor with attributes is the hook over the storage.
A param: list on set or init declares the hook's parameter, with the
property's type, and goes on it.

Three have no PHP place, and each is a compile error that says why:
return:, since PHP keeps no attributes on a return value; an accessor of
a get-only or static auto-property, which PHP runs without accessors,
and willSet or didSet, which run inside set; and a type parameter,
which PHP erases.

Spec section 1 states every target and place, with checked examples.
A target on an enum, its case and its computed property, a struct, an
event and both lambda forms now meets check_attribute_target in the
checker's tests, and an interface property's set hook takes its param:
attributes in the lowering's.
A target on an operator or a named constructor names its method, and a
declaration with no part to name reports an error instead of panicking.
An event's and a data case's parameters declare properties, so they
take param:, property: and field:, and a targeted list declares the
property beside its parameter. event: on an event is its own target,
and an attributed lambda without parentheses is one error.

Every accessor form takes attributes. One rule, has_hooks on the
accessor list, decides what runs as a hook, and the scanner reads it,
so a Map's index writes and remove reach the storage through an
attributed auto accessor. A static auto-property's attributed accessors
run as its hidden methods, and a get-only one's is a hook on a
protected(set) property.

Plain PHP that passes names to a PHP# ... parameter throws on entry,
overrides included. A read of a property __get serves is Any? after a
write, a declared property the code can't read is the __get property,
and a write to an undocumented one warns nothing. A repeated attribute
on a promoted parameter counts on both the parameter and the property.
Messages and spec sections 1, 14.3 and 16 say each rule in PHP# words.
# Conflicts:
#	sharp/mago/crates/semantics/src/internal/checker/sharp.rs
#	sharp/mago/crates/sharp-bridge/src/lower.rs
#	sharp/mago/crates/sharp-bridge/tests/lower.rs
@heyJordanParker
heyJordanParker merged commit 27626cb into master Oct 18, 2026
24 checks passed
heyJordanParker added a commit that referenced this pull request Oct 18, 2026
#159 declares an event's or a case's properties beside their parameters
when an attribute names a target. This branch does so when a parameter
holds a Map or a Set at any depth. promoted_constructor keeps both
reasons, as class_members does, and declares each property through
promoted_property. The semantics test keeps both sides: a Set event
parameter and an attribute on an event are both accepted.
heyJordanParker added a commit that referenced this pull request Oct 18, 2026
…d beside the permanent refusals

Problem

#159, #165, #168, #170 and #171 landed on master. #159 gives attributes
to interfaces, events, constants, accessors and lambdas, #168 gives
every pattern type a test, and the collections work builds tuple and
`Iterable` constants. #166's refusals of those forms as "not supported
yet" are now refusals of code that compiles. `==` on an `Iterable` was
still "not supported yet", and a `.sharp` file the engine refused stayed
in PHP's included files, so a second `require_once` skipped it and a
later use said "Class not found".

Solution

The merge keeps master's attribute arms, its `as` arm and its
attribute-argument refusal, keeps `report_accessor` with #159's
observer-attribute arm and its `ACCESSORS` note, and drops the arms
master made unreachable: `is_undecided`'s attribute lists, pattern
types with no test and tuple constants.

`==` on an `Iterable`, or on a type parameter bound by a collection,
is a permanent error that names `toList()`: "`==` compares a `List`, a
`Map` or a `Set`, and an `Iterable` is none of them: compare
`a.toList() == b.toList()`." A search and a struct property say the
same.

A `.sharp` file the engine refuses leaves PHP's included files, so a
second `require_once` reports the same refusal.

Spec section 20's `case-set-field` states its result, `// true`, which
the spec tool now runs.

php-sharp/
├── Zend/tests/sharp/refusal_required_again.phpt     a refused file is required again
├── docs/spec.md                                      case-set-field gives true
├── ext/sharp/sharp.c                                 sharp_compile_file drops a refused file
└── sharp/mago/crates/
    ├── analyzer/
    │   ├── src/expression/binary/comparison.rs       `==` on an Iterable names toList()
    │   └── tests/sharp.rs                            its rows, and a row that compiles toList()
    ├── hir/src/lower/error/mod.rs                    both sides' error list
    └── semantics/
        ├── src/internal/checker/sharp.rs             is_undecided, report_accessor, the as arm
        └── tests/sharp.rs                            rows for what master builds
@heyJordanParker
heyJordanParker deleted the fix/plain-php-members branch November 3, 2026 22:08
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.

1 participant