Encapsulation and Polymorphism in PHP
Anonymous
php
06/08/2023 9:29 PM
720 B
19
Indexable
<?php
// Encapsulation
function animalVoice($animal) {
switch ($animal) {
case 'dog':
echo "The dog barks.";
break;
case 'cat':
echo "The cat meows.";
break;
default:
echo "The animal speaks.";
break;
}
}
// Polymorphism
animalVoice('animal'); // Output: The animal speaks.
echo "<br>";
animalVoice('dog'); // Output: The dog barks.
echo "<br>";
animalVoice('cat'); // Output: The cat meows.
echo "<br>";
?>Editor is loading...