Skip to content

Instantly share code, notes, and snippets.

@Div-Man
Forked from kamrankr/polymorphism.php
Created May 22, 2023 22:16
Show Gist options
  • Save Div-Man/04d7b50dc68e1b9781f143f680fa8eff to your computer and use it in GitHub Desktop.
Save Div-Man/04d7b50dc68e1b9781f143f680fa8eff to your computer and use it in GitHub Desktop.
simplest Polymorphism example in Php
<?php
/* simplest Polymorphism example in Php */
abstract class Animal
{
static function makeSound(Animal $animal)
{
return $animal->getsound();
}
}
class dog extends Animal
{
function getsound()
{
return "Woof Woof";
}
}
class cat extends Animal
{
function getsound()
{
return "Meow Meow";
}
}
echo Animal::makeSound(new cat);
echo Animal::makeSound(new dog);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment