Skip to content

Instantly share code, notes, and snippets.

@tomphp
Last active December 29, 2015 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomphp/7615002 to your computer and use it in GitHub Desktop.
Save tomphp/7615002 to your computer and use it in GitHub Desktop.
<?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