php / php/php-src

Trait property conflicts silently drop attributes

Aperta
#19,466 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Status: Needs Triage
Lingua principale
C
Stelle
40.4k
Fork
8.1k
Merge medio
2g 13h
PR unite (30g)
96

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la riproduzione che coinvolge BasicTrait, VersionedTrait e ReflectionClass ed eseguila nell’ambiente PHP 8.2.28 segnalato per osservare la composizione delle proprietà dei trait. Segui il percorso di composizione dei trait dell’interprete PHP e aggiungi una copertura che dimostri che attributi di proprietà differenti producono la diagnostica prevista senza perdere silenziosamente informazioni.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
php
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.