Advertisement
benshepherd

PHP Constants

Dec 23rd, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.09 KB | None | 0 0
  1. <?php
  2.    
  3.     $animal = new Animal("Fooey", 9, Animal::MALE, Animal::DOG);
  4.     echo $animal->toString();
  5.  
  6.     class Animal
  7.     {
  8.         private $name;
  9.         private $age;
  10.         private $sex;
  11.         private $type;
  12.        
  13.         const MALE = 0;
  14.         const FEMALE = 0;
  15.  
  16.         const CAT = 0;
  17.         const DOG = 1;
  18.         const FOX = 2;
  19.         const HORSE = 3;
  20.  
  21.         function __construct($name, $age, $sex, $type)
  22.         {
  23.             $this->name = $name;
  24.             $this->age = $age;
  25.             $this->sex = $sex;
  26.             $this->type = $type;
  27.         }
  28.  
  29.         public function toString()
  30.         {
  31.             return $this->name.", is a ".$this->getSex()." ".$this->getType()." and is ".$this->age." years of age.";
  32.         }
  33.        
  34.         protected function getSex()
  35.         {
  36.             switch($this->sex)
  37.             {
  38.                 case Animal::MALE:
  39.                
  40.                     return "Male";
  41.                 case Animal::FEMALE:
  42.                    
  43.                     return "Female";   
  44.             }
  45.         }
  46.  
  47.         protected function getType()
  48.         {
  49.             switch($this->type)
  50.             {
  51.                 case Animal::CAT:
  52.  
  53.                     return "Cat";
  54.                 case Animal::DOG:
  55.  
  56.                     return "Dog";
  57.                 case Animal::FOX:
  58.  
  59.                     return "Fox";
  60.                 case Animal::HORSE:
  61.  
  62.                     return "Horse";
  63.                 default:
  64.  
  65.                     return "Unspecified";
  66.             }
  67.         }
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement