Skip to content

Properties (property hooks) composition #18391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
SerafimArts opened this issue Apr 22, 2025 · 3 comments
Open

Properties (property hooks) composition #18391

SerafimArts opened this issue Apr 22, 2025 · 3 comments

Comments

@SerafimArts
Copy link
Contributor

SerafimArts commented Apr 22, 2025

Description

There is code that implements behavior other than method declarations:

Methods Behaviour

<?php

trait ExpectsFooMethod
{
    abstract public function foo(): string;
}

trait ProvidesFooMethod
{
    use ExpectsFooMethod;

    public function foo(): string {}
}

final readonly class FooMethodImpl
{
    use ProvidesFooMethod;
}

Methods Result

See: https://3v4l.org/O5lIe#v8.4.6

// Nothing: OK

Properties Behaviour

<?php

trait ExpectsFooProperty
{
    abstract public string $foo { get; }
}

trait ProvidesFooProperty
{
    use ExpectsFooProperty;

    public readonly string $foo;
}

final readonly class FooPropertyImpl
{
    use ProvidesFooProperty;
}

Properties Result

See: https://3v4l.org/tM9i5#v8.4.6

Fatal error: ProvidesFooProperty and ExpectsFooProperty define the same hooked property ($foo) in the composition of ProvidesFooProperty. Conflict resolution between hooked properties is currently not supported. Class was composed in xxxx.php

PHP Version

PHP 8.4.5 (linux, windows); PHP 8.4.6 (from 3v4l.org)

Operating System

Windows, Ubuntu 20.04, Ubuntu 22.04

@iluuu1994
Copy link
Member

iluuu1994 commented Apr 22, 2025

Hi @SerafimArts. As the message says:

Conflict resolution between hooked properties is currently not supported.

Conflict resolution of hooks in traits (meaning, declaring the same property in both trait and target, at least one of them with hooks) is not supported. This was explicitly mentioned in the RFC:

https://wiki.php.net/rfc/property-hooks#interaction_with_traits

Properties in traits may declare hooks, just like any other property. However, as with normal properties, there is no conflict resolution mechanism provided the way methods have. If a trait and a class where it is used both declare the same property with hooks, an error is issued.

Hence, this is not a bug, and changing it would likely require an RFC.

@SerafimArts
Copy link
Contributor Author

SerafimArts commented Apr 22, 2025

From the user's point of view, there is only one declaration in the code. An abstract property declaration is not a declaration, but a requirement for a declaration. Moreover, the implementation is already defined as a readonly field, not a property (hook).

This leaves us with 2 problems (mistakes?):

  • The error message relies on the implementation, not the actual code:
    • Reports that the property is being overwritten by the property, however, in the code there is an abstract property that is implemented through a readonly field (there is no rewriting, semantically).
  • The RFC also relies on the implementation, not the declared behavior:
    • Perhaps the implementation of this functionality simply did not provide for the abstract properties and they are simply considered in the same way as the implementation, leaving this functionality for future improvements (as stated in the RFC).

@SerafimArts
Copy link
Contributor Author

SerafimArts commented Apr 22, 2025

P.S. I do not argue that it does not work correctly. It just seems that this functionality can be implemented without RFC in 8.5, since semantically (depending on the RFC interpretation), fixing this behavior falls within the scope of this same RFC. Since "technically" there are no real property declarations in code above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants