} protected function setHeight($height) { $this->height = $height; } protected function setWidth($width) { $this->width = $width; } protected function getHeight() { return $this->height; } protected function getWidth() { return $this->width; } public function area() { return $this->getHeight() * $this->getWidth(); } public function perimeter() { return $this->getHeight() * 2 + $this->getWidth() * 2; } } $rectangle = new Rectangle(10, 10); echo $rectangle->area(); echo $rectangle->getHeight(); echo $rectangle->getWidth();
/** * Определяет, вложен ли текущий прямоугольник в заданный. * * @param Rectangle $rect * @return type */ public function isInner(Rectangle $rect) { return $this->getTop() >= 0 && $this->getLeft() >= 0 && $this->getBottom() <= $rect->getHeight() && $this->getRight() <= $rect->getWidth(); }
<?php require_once 'rectangle.php'; require_once 'square.php'; $rectangle = new Rectangle(10, 10); echo $rectangle->area() . PHP_EOL; echo "The area of a rectangle with height of {$rectangle->getHeight()} and width of {$rectangle->getWidth()} is {$rectangle->area()} " . PHP_EOL; echo "The perimeter of a rectangle with height of {$rectangle->getHeight} and width of {$rectangle->getWidth()} is {$rectangle->perimeter()} " . PHP_EOL; // $square = new Square(5); // echo "The area of a square with height of {$square->height} is {$square->area()} " . PHP_EOL; // echo "The perimeter of a square with height of {$square->height} is {$square->perimeter()} " . PHP_EOL;