php / php/php-src

Trait property conflicts silently drop attributes

オープン
#19,466 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Bug Status: Needs Triage
主要言語
C
スター
40.4k
フォーク
8.1k
平均マージ
2日 13時間
マージ済み PR(30日)
96

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

BasicTrait、VersionedTrait、ReflectionClassを含む再現から始め、報告されたPHP 8.2.28環境で実行して、traitのプロパティ合成を観察します。PHPインタプリタのtrait合成経路を追跡し、異なるプロパティ属性が情報を暗黙に失うことなく意図した診断を生成することを示すカバレッジを追加します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
php
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。