php / php/php-src

Trait property conflicts silently drop attributes

Đang mở
#19,466 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Bug Status: Needs Triage
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
Summary

When multiple traits define properties with identical signatures but different attributes, PHP silently drops attributes during trait composition without any notice or warning.

Affected versions: PHP 8.0+ (since attributes were introduced)

Problem Description

PHP's trait system correctly handles method conflicts by throwing fatal errors when methods with the same name exist in multiple traits. However, when properties with identical signatures but different attributes are defined in multiple traits, PHP:

  1. Correctly merges the property (no fatal error)
  2. Silently drops attributes from one of the traits
  3. Provides no indication that attributes were lost
  4. Behavior is unpredictable (depends on trait order)

This creates silent data loss where code appears correct but attributes are missing.

Expected Behavior

When trait properties have identical signatures but different attributes, PHP should at minimum emit a notice or warning:

Notice: Trait property 'propertyName' has conflicting attributes during trait composition in %s on line %d
Reproduction Code
<?php

#[Attribute]
class Column {}

#[Attribute] 
class Version {}

// Trait 1: Property with Column attribute
trait BasicTrait {
    #[Column]
    private ?string $data = null;
}

// Trait 2: Same property with Column AND Version attributes
trait VersionedTrait {
    #[Column]
    #[Version]
    private ?string $data = null;
}

class TestEntity {
    use BasicTrait;
    use VersionedTrait; // Version attribute should be preserved
}

// Check what actually happened
$reflection = new ReflectionClass(TestEntity::class);
$property = $reflection->getProperty('data');
$attributes = $property->getAttributes();

echo "Total attributes: " . count($attributes) . "\n";
foreach ($attributes as $attr) {
    echo "- " . $attr->getName() . "\n";
}

$versionAttrs = $property->getAttributes(Version::class);
echo "Version attributes: " . count($versionAttrs) . "\n";

if (count($versionAttrs) === 0) {
    echo "BUG: Version attribute silently dropped!\n";
}
Expected Output
Notice: Trait property 'data' has conflicting attributes during trait composition in %s on line %d
Total attributes: 1
- Column
Version attributes: 0
BUG: Version attribute silently dropped!
Actual Output
Total attributes: 1
- Column
Version attributes: 0
BUG: Version attribute silently dropped!
Why This Is a Bug
  • Inconsistent: Method conflicts properly throw fatal errors
  • Silent data loss: PHP usually warns about potential issues
  • Unpredictable: Same code behaves differently based on trait order
  • No workaround: Cannot use insteadof or as with properties
Proposed Fix

Emit a notice/warning when attributes are dropped during trait property merging (minimum viable fix).

PHP Version
PHP 8.2.28 (cli) (built: Mar 11 2025 17:58:12) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.28, Copyright (c) Zend Technologies
    with Xdebug v3.3.2, Copyright (c) 2002-2024, by Derick Rethans
    with Zend OPcache v8.2.28, Copyright (c), by Zend Technologies
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.

Hướng nghiên cứu

Bắt đầu với bản tái hiện liên quan đến BasicTrait, VersionedTrait và ReflectionClass, rồi chạy nó trên môi trường PHP 8.2.28 đã được báo cáo để quan sát quá trình hợp thành thuộc tính của trait. Truy vết đường đi hợp thành trait của trình thông dịch PHP và bổ sung độ bao phủ cho thấy các thuộc tính khác nhau của thuộc tính tạo ra chẩn đoán như dự định mà không âm thầm làm mất thông tin.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
php
Lĩnh vực
compilers
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
38/100

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.