Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.47 KB | None | 0 0
  1.  class base  implements JsonSerializable {
  2.  
  3.   public function jsonSerialize() {
  4.    return get_object_vars($this);}
  5.    public function Area()
  6.   {
  7.  
  8.   }
  9.   }
  10. //include("base.inc");
  11.   class Circle  extends base {
  12.  
  13.         public $type = circle;
  14.         public function __construct( $r)
  15.     {
  16.         $this->radius = $r;
  17.     }
  18.    
  19.       public function show_one() {
  20.  
  21.           echo $this->one;
  22.           echo $this->radius;
  23.  
  24.       }
  25.       public function Area()
  26.     {
  27.         return pi() * pow($this->radius, 2);
  28.     }
  29.     public function __toString() {
  30.         return  "radius: {$this->radius}\n";
  31.     }
  32.      /*public function jsonSerialize() {
  33.      //return get_object_vars($this);}
  34.         return [
  35.             'type' => $this->type,
  36.             'radius' => $this->radius,
  37.             'ploshad' => $this->Area()
  38.         ];}
  39.         */
  40.   }
  41. class Rectangle extends base
  42. {
  43.   private $width;
  44.   private $height;
  45.   public  $type= Rectangle;
  46.  
  47.   public function __construct($x, $y)
  48.   {
  49.     $this->width = $x;
  50.     $this->height = $y;
  51.   }
  52.  
  53.  
  54.  
  55.  
  56.   public function Area()
  57.   {
  58.     return $this->width * $this->height;
  59.   }
  60.  /* public function jsonSerialize() {
  61.    //return get_object_vars($this);}
  62.         return [
  63.             'type' => $this->type,
  64.             'a' => $this->width,
  65.             'b' => $this->height,
  66.             'ploshad' => $this->Area()
  67.         ];}*/
  68. }
  69.  $a = new Circle(rand(1,10));
  70.   $b = new Rectangle(rand(1,10),rand(1,10));
  71. obj_decode($a);
  72.  $d = json_encode($a);
  73.  $d= $d . ',' . json_encode($b);
  74.  $d='['.$d.']';
  75. var_dump($d);
  76.  file_put_contents('store.json', $d);
  77. $path = file_get_contents("store.json");
  78. $json = json_decode($path, true);
  79. /*
  80. for($i=0;$i<count($json);$i++) {
  81.     switch ($json[$i]['type']) {
  82.         case "circle":
  83.             $figure = new Circle($json[$i]['radius']);
  84.             break;
  85.         case "rectangle":
  86.             $figure = new Rectangle($json[$i]['a'], $json[$i]['b']);
  87.             break;
  88.     }
  89.     $json[$i]['s'] = $figure->Area();
  90. }
  91. */
  92. usort($json, function($a, $b) {
  93.     if($a['s'] === $b['s'])
  94.         return 0;        
  95.     return $a['s'] > $b['s'] ? -1 : 1;
  96. });
  97.  
  98. for($i=0;$i<count($json);$i++) {
  99.     switch ($json[$i]['type']) {
  100.         case "circle":
  101.             echo "Круг. Рудиус=".$json[$i]['radius'].". Площадь:\n".$json[$i]['ploshad']."</br>";
  102.             break;
  103.             case "Rectangle":
  104.             echo "Прямоугольник. a=".$json[$i]['a'].", b=".$json[$i]['b'].". Площадь:\n".$json[$i]['ploshad']."</br>";
  105.             break;
  106.      
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement