php / php/php-src

Abstract properties (hooks) in traits

Open
#19,504 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Category: Engine Feature Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

At the moment such code does not work (8.4 and 8.5/master), which does not seem very logical:

<?php

trait Example
{
    abstract public string $property {
        get; 
    }
}

class ExampleImpl
{
    use Example;
    
    public function __construct(
        public readonly string $property,
    ) {}
}

Produces:

Fatal error: ExampleImpl and Example define the same hooked property ($property) in the composition of ExampleImpl. Conflict resolution between hooked properties is currently not supported. Class was composed

https://3v4l.org/UYnbo

According to RFC (https://wiki.php.net/rfc/property-hooks#interaction_with_traits), traits do not actually support properties, but at least such primitive conflict resolution would be nice to have.


P.S. Is it worth writing to internals for RFC if there is no way to send a PR?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided trait and hooked-property reproducer, then read the property-hooks RFC section on trait interaction. Trace how trait composition handles the abstract hooked property and the implementing constructor-promoted property. Done means the example composes without the reported conflict while preserving the intended property-hook behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.