Advertisement
tuaris

Abstract Properties in PHP

Dec 21st, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.06 KB | None | 0 0
  1. <?php
  2. abstract class Art {
  3.     public static function getDescription(){
  4.         if(!isset(static::$Description)) {throw new UnexpectedValueException(get_called_class() . ' must define a static Description property');}
  5.         return static::$Description;
  6.     }
  7. }
  8.  
  9. interface Drawing{
  10.     public function Draw();
  11.     public function Delete();
  12. }
  13.  
  14. abstract class Shape extends Art implements Drawing {
  15.     abstract public function Draw();
  16.     public function Delete(){echo "Deleted the " . static::getDescription() . "\n";}
  17. }
  18.  
  19. class Square extends Shape {
  20.     protected static $Description = "Square";
  21.     public function Draw(){echo "Drawing something with four sides....\n";}
  22. }
  23. class Circle extends Shape {
  24.     protected static $Description = "Circle";
  25.     public function Draw(){echo "Drawing something round....\n";}
  26. }
  27. ?>
  28.  
  29. <pre>
  30. <?php
  31. echo "Avialable Items to draw:\n";
  32. echo Square::getDescription() . ", " . Circle::getDescription() . "\n\n";
  33.  
  34. $shape1 = new Square();
  35. $shape2 = new Circle();
  36.  
  37. $shape1->Draw();
  38. $shape2->Draw();
  39. $shape1->Delete();
  40. $shape2->Delete();
  41. ?>
  42. </pre>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement