Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class base implements JsonSerializable {
- public function jsonSerialize() {
- return get_object_vars($this);}
- public function Area()
- {
- }
- }
- //include("base.inc");
- class Circle extends base {
- public $type = circle;
- public function __construct( $r)
- {
- $this->radius = $r;
- }
- public function show_one() {
- echo $this->one;
- echo $this->radius;
- }
- public function Area()
- {
- return pi() * pow($this->radius, 2);
- }
- public function __toString() {
- return "radius: {$this->radius}\n";
- }
- /*public function jsonSerialize() {
- //return get_object_vars($this);}
- return [
- 'type' => $this->type,
- 'radius' => $this->radius,
- 'ploshad' => $this->Area()
- ];}
- */
- }
- class Rectangle extends base
- {
- private $width;
- private $height;
- public $type= Rectangle;
- public function __construct($x, $y)
- {
- $this->width = $x;
- $this->height = $y;
- }
- public function Area()
- {
- return $this->width * $this->height;
- }
- /* public function jsonSerialize() {
- //return get_object_vars($this);}
- return [
- 'type' => $this->type,
- 'a' => $this->width,
- 'b' => $this->height,
- 'ploshad' => $this->Area()
- ];}*/
- }
- $a = new Circle(rand(1,10));
- $b = new Rectangle(rand(1,10),rand(1,10));
- obj_decode($a);
- $d = json_encode($a);
- $d= $d . ',' . json_encode($b);
- $d='['.$d.']';
- var_dump($d);
- file_put_contents('store.json', $d);
- $path = file_get_contents("store.json");
- $json = json_decode($path, true);
- /*
- for($i=0;$i<count($json);$i++) {
- switch ($json[$i]['type']) {
- case "circle":
- $figure = new Circle($json[$i]['radius']);
- break;
- case "rectangle":
- $figure = new Rectangle($json[$i]['a'], $json[$i]['b']);
- break;
- }
- $json[$i]['s'] = $figure->Area();
- }
- */
- usort($json, function($a, $b) {
- if($a['s'] === $b['s'])
- return 0;
- return $a['s'] > $b['s'] ? -1 : 1;
- });
- for($i=0;$i<count($json);$i++) {
- switch ($json[$i]['type']) {
- case "circle":
- echo "Круг. Рудиус=".$json[$i]['radius'].". Площадь:\n".$json[$i]['ploshad']."</br>";
- break;
- case "Rectangle":
- echo "Прямоугольник. a=".$json[$i]['a'].", b=".$json[$i]['b'].". Площадь:\n".$json[$i]['ploshad']."</br>";
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement