Skip to content

Instantly share code, notes, and snippets.

@garveen
Last active February 21, 2017 08:49
Show Gist options
  • Save garveen/d9045669e5eb25916036fd30cf418cf0 to your computer and use it in GitHub Desktop.
Save garveen/d9045669e5eb25916036fd30cf418cf0 to your computer and use it in GitHub Desktop.
<?php
class Base
{
private $called = false;
public function save()
{
if ($this->called) {
throw new Exception("should be only called once!", 1);
}
$this->called = true;
}
}
trait TraitA
{
public function save()
{
parent::save();
}
}
trait TraitB
{
public function save()
{
parent::save();
}
}
class ExtA extends Base
{
use TraitA;
}
class ExtB extends Base
{
use TraitB;
}
class ExtC extends Base
{
use TraitA, TraitB {
TraitA::save as saveA;
TraitB::save as saveB;
}
function save()
{
$this->saveA();
$this->saveB();
}
}
$a = new ExtA;
$a->save();
$b = new ExtB;
$b->save();
$c = new ExtC;
$c->save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment