Encapsulation and Polymorphism in PHP

 avatar
unknown
php
2 years ago
539 B
6
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...