Created
August 26, 2023 03:51
-
-
Save mkhalid03/dde7a72b1682936ae2b21e12de8a62f4 to your computer and use it in GitHub Desktop.
Managing Method Conflicts in PHP Traits: Resolving Conflicts with as Keywords
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
trait TraitA { | |
public function commonMethod() { | |
echo "Method from TraitA\n"; | |
} | |
} | |
trait TraitB { | |
public function commonMethod() { | |
echo "Method from TraitB\n"; | |
} | |
} | |
class MyClass { | |
use TraitA, TraitB { | |
TraitA::commonMethod as methodFromTraitA; // Use TraitA's method | |
TraitB::commonMethod as methodFromTraitB; // Rename TraitB's method | |
} | |
public function commonMethod() { | |
echo "Method from MyClass\n"; | |
} | |
} | |
$obj = new MyClass(); | |
$obj->commonMethod(); // Output: Method from Class | |
$obj->methodFromTraitA(); // Output: Method from TraitA | |
$obj->methodFromTraitB(); // Output: Method from TraitB | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment