Encapsulation and Polymorphism in PHP

mail@pastecode.io avatar
unknown
php
6 months ago
539 B
2
Indexable
Never
<?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>";
?>