php / php/php-src

Properties (hooks) and inheritance

Đang mở
#15,424 6 bình luận 2 reaction 1 người được giao Xem trên GitHub

@iluuu1994 đang làm issue này rồi.

Từ ngày 15/8/2024.

Category: Engine Feature Status: Verified
Ngôn ngữ chính
C
Star
40.4k
Fork
8.1k
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
96

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.