Skip to content

PHP 8.4: dynamic dispatch problem with traits and inheritance #18174

Closed
@procodix

Description

@procodix

Description

I have the following bug. I think it might by a corner case. It is very hard to narrow down. Please find this reduces example. In my real code many more traits are involved, as are promoted properties, property hooks, etc. I cannot pinpoint the error to a specific version, but Im sure, it worked correctly in PHP 8.3.

trait X {
    public function test() {echo 'this is from A';}
}

class A {
    use X;
}

class B extends A {
    public function test() {echo 'this is from B';}
}

class C extends B {
	public function run() {
		$this->test(); // this outputs 'this is from A' !!! 
	}
	
	// however if I include this function, it works correctly by echoing 'this is from B'
	// public function test() {
	// 	parent::test();
	// }

	// whats even more weird, is that an almost identical class D works correctly
}

So the call to C->test() skips the inherited B->test() and dispatches to A/X->test().

I ruled out, that

  • a rouge "test" function exists, that takes over
  • visibility problems with public
  • a wrong class is used because of a namespace inaccuracy

Most interesting is, that the pure existence of the called method in C with an explicit call to the parent clears the issue. As this should be redundant, I assume a bug.

Please let me know, if I should provide more details. As it is very difficult to reduce the code to a non-working example, thought, maybe something changed in PHP 8.4 regarding dynamic dispatch which could lead to this behavior.

PHP Version

PHP 8.4

Operating System

macOS 15

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions