Skip to content

Instantly share code, notes, and snippets.

@PululuK
Last active May 22, 2020 21:03
Show Gist options
  • Save PululuK/37794c60d429bd90cc1887d26b021ed3 to your computer and use it in GitHub Desktop.
Save PululuK/37794c60d429bd90cc1887d26b021ed3 to your computer and use it in GitHub Desktop.
polymorphisme concept
class T
{
private $type;
public function __construct(A $a)
{
$this->setType($a);
}
public function getType()
{
return $this->type->getType();
}
public function setType(A $a)
{
$this->type = $a;
}
}
interface A
{
public function getType();
}
class B implements A
{
public function getType()
{
echo __CLASS__, PHP_EOL;
}
}
class C implements A
{
public function getType()
{
echo __CLASS__, PHP_EOL;
}
}
$b = new B();
($t = new T($b))->getType();
$c = new C();
$t->setType($c);
$t->getType();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment