3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait implementedBar { public function bar($a){ echo "a"; } } trait abstractBar { abstract protected function bar($a); } abstract class parentImplemented { use implementedBar; } class childAbstract extends parentImplemented { use abstractBar; } //what would happen if you copy/pasted the traits into their classes //the below classes would definitely cause an error /** abstract class parentImplemented { public function bar($a){ echo "a"; } } class childAbstract extends parentImplemented { abstract protected function bar($a); } **/ //below will just work, but we can't really see that implementedBar::bar is an implementation of abstractBar::bar //instead of it being a replacement, or abstractBar::bar just getting ignored echo "Example 1:".PHP_EOL; $b = new childAbstract; $b->bar([]); //////////////////////////////////// trait implementedBarTH { public function bar(array $a){ echo "a"; } } trait abstractBarTH { abstract protected function bar(int $a); } abstract class parentImplementedTH { use implementedBarTH; } class childAbstractTH extends parentImplementedTH { use abstractBarTH; } //again, a version with the traits copy/pasted into their classes //definitely will cause an error /** abstract class parentImplementedTH { public function bar(array $a){ echo "a"; } } class childAbstractTH extends parentImplementedTH { abstract protected function bar(int $a); } **/ //This will give us an error related to mismatched type hints. //This shows that the method in the trait used in the parent class is definitely //acting as an implementation of the abstract class from the trait used in the child class $b = new childAbstractTH; $b->bar([]); /*** You'll need to comment out 53-59, 80-81 in order to produce an error for the items below ***/ ////////////////////////////////////////////////////////////// class childBothTH { use abstractBarTH, implementedBarTH; } //a version with the traits copy/pasted into the class //definitely will cause an error /** class childBothTH { abstract protected function bar(int $a); public function bar(array $a){ echo "a"; } } **/ //This will give us an error related to mismatched type hints. //This shows that the method in the trait that was implemented is definitely //acting as an implementation of the abstract class from the other trait $b = new childBothTH; $b->bar([]);
Output for git.master_jit, git.master, rfc.property-hooks
Example 1: a Fatal error: Declaration of parentImplementedTH::bar(array $a) must be compatible with abstractBarTH::bar(int $a) in /in/OKoTY on line 44
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
53.35 ms | 401 KiB | 8 Q