benshepherd icon

PHP Constants

benshepherd | PRO | 12/23/13 10:13:15 PM UTC | 0 ⭐ | 304 👁️ | Never ⏰ | []
PHP |

1.09 KB

|

None

|

0 👍

/

0 👎

<?php 
    
    $animal = new Animal("Fooey", 9, Animal::MALE, Animal::DOG);
    echo $animal->toString();
 
    class Animal
    {
        private $name;
        private $age;
        private $sex;
        private $type;
        
        const MALE = 0;
        const FEMALE = 0;
 
        const CAT = 0;
        const DOG = 1;
        const FOX = 2;
        const HORSE = 3;
 
        function __construct($name, $age, $sex, $type)
        {
            $this->name = $name;
            $this->age = $age;
            $this->sex = $sex;
            $this->type = $type;
        }
 
        public function toString()
        {
            return $this->name.", is a ".$this->getSex()." ".$this->getType()." and is ".$this->age." years of age.";
        }
        
        protected function getSex()
        {
            switch($this->sex)
            {
                case Animal::MALE:
                
                    return "Male";
                case Animal::FEMALE:
                    
                    return "Female";    
            }
        }
 
        protected function getType()
        {
            switch($this->type)
            {
                case Animal::CAT:
 
                    return "Cat";
                case Animal::DOG:
 
                    return "Dog";
                case Animal::FOX:
 
                    return "Fox";
                case Animal::HORSE:
 
                    return "Horse";
                default:
 
                    return "Unspecified";
            }
        }
    }

Comments