php / php/php-src

Properties (hooks) and inheritance

Abierto
#15,424 6 comentarios 2 reacciones 1 asignado Ver en GitHub

@iluuu1994 ya está trabajando en esto.

Desde el 15/8/2024.

Category: Engine Feature Status: Verified
Lenguaje dominante
C
Estrellas
40.4k
Forks
8.1k
Merge medio
2 d 13 h
PR fusionados (30 d)
96

Descripción

Description

Referenced RFC https://github.com/php/php-src/pull/13455

The following code:

<?php

interface ExampleInterface {} // parent
interface MutableExampleInterface extends ExampleInterface {} // child
final class Example implements MutableExampleInterface {} // impl

// parent provider
interface ExampleProviderInterface
{
    // Any type that implements ExampleInterface can be read
    public ExampleInterface $property { get; }
}

// child provider that "overrides" parent definition
interface MutableExampleProviderInterface extends ExampleProviderInterface
{
    // Any type that implements (MutableExampleInterface & ExampleInterface) can be read + write
    // We supplement the object with the set method
    public MutableExampleInterface $property { get; set; }
}

// An implementation that uses implementation
final class ExampleProvider implements MutableExampleProviderInterface
{
    public Example $property { // << implementation
        // We can return the implementation, because it implements (MutableExampleInterface & ExampleInterface)
        get => $this->property; 
        // Any instance of ExampleInterface is allowed for set, since it satisfies the covariance condition
        set (ExampleInterface $v) => $this->property = new Example($v);
    }
}

Resulted in this output:

Fatal error: Type of ExampleProvider::$property must be MutableExampleInterface (as in class MutableExampleProviderInterface)

But I expected this output instead:

*nothing*

Same code (logic) using getters and setters instead of properties: https://onlinephp.io/c/4e180

interface ExampleInterface {}
interface MutableExampleInterface extends ExampleInterface {}
final class Example implements MutableExampleInterface {}

interface ExampleProviderInterface
{
    public function getProperty(): ExampleInterface;
}

interface MutableExampleProviderInterface extends ExampleProviderInterface
{
    public function getProperty(): MutableExampleInterface;
    public function setProperty(MutableExampleInterface $value): void;
}

final class ExampleProvider implements MutableExampleProviderInterface
{
    public function getProperty(): Example {}
    public function setProperty(ExampleInterface $value): void {}
}

Ping @iluuu1994 (again) ^_^

PHP Version

PHP 8.4-beta1

Operating System

No response

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.