} public function setSideLength($length) { $this->_sideLength = $length; } public function getArea() { return pow($this->_sideLength, 2); } } $myCircle = new Circle(); $myCircle->setColor("red"); $myCircle->fill(); $myCircle->setRadius(4); echo "<h2>My Circle</h2>"; echo "<p>My circle has a radius of " . $myCircle->getRadius() . ".</p>"; echo "<p>It is " . $myCircle->getColor() . " and it is " . ($myCircle->isFilled() ? "filled" : "hollow") . ".</p>"; echo "<p>The area of my circle is: " . $myCircle->getArea() . ".</p>"; $mySquare = new Square(); $mySquare->setColor("green"); $mySquare->makeHollow(); $mySquare->setSideLength(3); echo "<h2>My Square</h2>"; echo "<p>My square has a side length of " . $mySquare->getSideLength() . ".</p>"; echo "<p>It is " . $mySquare->getColor() . " and it is " . ($mySquare->isFilled() ? "filled" : "hollow") . ".</p>"; echo "<p>The area of my square is: " . $mySquare->getArea() . ".</p>"; ?> </body> </html>
<?php // PHP exercise about extending classes from a parent class. Rectangle is the parent class. // Square is the child class extending off of Rectangle. // This file runs in the command line. // Because Square.php requires Rectangle.php, this file has access to both. require_once 'Square.php'; $rectangle = new Rectangle(5, 10); echo 'Rectangle area: ' . $rectangle->getArea() . PHP_EOL; echo 'Rectangle perimeter: ' . $rectangle->getPerimeter() . PHP_EOL; $square = new Square(9); echo 'Square area: ' . $square->getArea() . PHP_EOL; echo 'Square perimeter: ' . $square->getPerimeter() . PHP_EOL;
<?php /** * Created by PhpStorm. * User: ET * Date: 6/26/2015 * Time: 4:49 PM */ include 'Square.php'; $width = 21; $height = 98; $r = new Rectangle($width, $height); echo '<p>The area of the rectangle is ' . $r->getArea() . '</p>'; echo '<p>The perimeter of the rectangle is ' . $r->getPerimeter() . '</p>'; $side = 60; $s = new Square($side); echo '<p>The area of the square is ' . $s->getArea() . '</p>'; echo '<p>The perimeter of the square is ' . $s->getPerimeter() . '</p>';
<?php require_once "rectangle.php"; require_once "square.php"; $rectangle = new Rectangle(15, 5); $square = new Square(10); echo $rectangle->getArea() . PHP_EOL; echo $square->getArea() . PHP_EOL; echo $square->getPerimeter() . PHP_EOL;
<?php require_once 'square.php'; $rectangle = new Rectangle(4, 5); echo 'The area is: ' . $rectangle->getArea() . PHP_EOL; $square = new Square(4); echo 'The area is: ' . $square->getArea() . PHP_EOL; echo 'The perimeter is: ' . $square->perimeter() . PHP_EOL;