Last active
December 29, 2015 04:29
-
-
Save tomphp/7615002 to your computer and use it in GitHub Desktop.
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 MyTrait | |
{ | |
public function theFunc() | |
{ | |
echo 'Trait func'; | |
} | |
} | |
class Base | |
{ | |
public function theFunc() | |
{ | |
echo "Base func"; | |
} | |
} | |
class MyClass extends Base | |
{ | |
use MyTrait; | |
public function theFunc() | |
{ | |
// Doesn't work, says calling a non-static method statically | |
MyTrait::theFunc(); | |
// However the following does work so I don't see why this shouldn't | |
// be treated similarly | |
Base::theFunc(); | |
} | |
} | |
// SOLUTION | |
class MyClass extends Base | |
{ | |
use MyTrait { theFunc as private theFuncFromTrait; } | |
public function theFunc() | |
{ | |
$this->theFuncFromTrait(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment