Gerard-Meier

Interfaces

Apr 21st, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.70 KB | None | 0 0
  1.  
  2.  
  3. interface Vehicle {
  4.     public function whatAreYou();
  5. }
  6.  
  7. class Plane implements Vehicle {
  8.     public function whatAreYou() {
  9.         print "I'm a plane.";
  10.     }
  11. }
  12.  
  13. class Bicycle implements Vehicle {
  14.     public function whatAreYou() {
  15.         print "I'm a bicycle.";
  16.     }
  17. }
  18.  
  19. // Declare $vehicle of the type "Vehicle". It can now hold any derivative of the "Vehicle" interface.
  20. Vehicle $vehicle;
  21.  
  22. // This is acceptable:
  23. $vehicle = new Bicycle();
  24.  
  25. // This works too!
  26. $vehicle = new Plane();
  27.  
  28. // But really none of this matters, since a PHP variable can hold anything anyway. but with languages as C++ or Java, a variable always has a type, so even a boolean wouldn't fit in an integer. Nor a float in a boolean (etc).
Advertisement
Add Comment
Please, Sign In to add comment