How to use two traits with duplicate method in PHP?

We can easily use multiple traits with duplicate method name with instance of like as follow

<?php
 trait  custom
 {
 public function hello()
 {
 echo "hello";
 }
 } trait custom2 { public function hello() { echo "hello2"; } } class inheritsCustom { use custom, custom2 { custom2::hello insteadof custom; } } $obj = new inheritsCustom(); $obj->hello(); ?> Output: hello2

 

Leave a Comment

Your email address will not be published. Required fields are marked *