function testGetArea() { // Need a Rectangle: $r = new Rectangle(8, 9); // The assertion tests the math: $this->assertEquals(72, $r->getArea()); }
public function test_getFullDescription() { $rectangle = new Rectangle(6, 7); $id = $rectangle->getId(); $name = str_shuffle(time()); $rectangle->name = $name; $this->assertEquals('Rectangle<#' . $id . '>: ' . $name . ' - 6 x 7', $rectangle->getFullDescription()); }
function areaVerifier(Rectangle $r) { $r->setWidth(5); $r->setHeight(4); if ($r->area() != 20) { return false; } return true; }
function saveRectangle() { if (!isset($_POST['topA']) || !isset($_POST['topB']) || !isset($_POST['leftA']) || !isset($_POST['leftB'])) { return array("success" => false, "error" => "Wrong formatted request"); } $rectangle = new Rectangle((int) $_POST['topA'], (int) $_POST['leftA'], (int) $_POST['topB'], (int) $_POST['leftB']); if ($rectangle->save()) { return array("success" => true); } return array("success" => false, "error" => $rectangle->isValid() ? "Oops, something went wrong when saving the rectangle" : "Rectangle with no width and height cannot be registered."); }
public function loadCache() { $rect = new Rectangle(); $rect->setId("1"); $this->shapeMap["1"] = $rect; $square = new Square(); $square->setId("2"); $this->shapeMap["2"] = $square; $circle = new Circle(); $circle->setId("3"); $this->shapeMap["3"] = $circle; }
<?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;
$this->setWidth($width); } 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();
<?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;
public function __construct($height) { parent::_construct($height, $height); }
/** * Определяет, вложен ли текущий прямоугольник в заданный. * * @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 getenv("DOCUMENT_ROOT") . "/lib/config.php"; require_once "Shape.php"; require_once "Arc.php"; require_once "Rectangle.php"; $arc = new Arc(10); echo $arc->calcS() . "<br/>"; echo $arc->calcP() . "<br/>"; $rect = new Rectangle(8, 6); echo $rect->calcS() . "<br/>"; echo $rect->calcP() . "<br/>";
public function __construct($side) { parent::__construct($side, $side); }
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Trait</title> <link rel="stylesheet" href="style.css"> </head> <body> <?php # Script 6.7 - trait.php // This page uses the tDebug trait through the Rectangle object. // Include the trait definition: // require('tDebug.php'); // Include the class definition: // require('Rectangle.php'); function __autoload($class) { require $class . '.php'; } // Create a new object: $r = new Rectangle(42, 37); // Dump the information: $r->dumpObject(); // Delete the object: unset($r); ?> </body> </html>
<?php require_once 'classes/rectangle.php'; $r1 = new Rectangle(); $r1->set_width(5); $r1->set_height(4); echo 'R1<br>Area: ' . $r1->get_area() . '<br>Perimeter: ' . $r1->get_perimeter(); $r2 = new Rectangle(8, 6); echo '<br>R1<br>Area: ' . $r2->get_area() . '<br>Perimeter: ' . $r2->get_perimeter();
function quantumLayoutParams(&$sizes, &$box, &$growWide) { global $debug; global $offset_y; if ($debug) { echo '<ul>'; echo '<li>'; $offset_y += 100; } $boxes = array(); $pivotIndex = $this->computePivotIndex($sizes); $pivotSize = $sizes[$pivotIndex]; $boxAR = $box->aspectRatio(); //echo "pivotIndex=$pivotIndex"; //echo '<br/>'; if (count($sizes) == 1) { $boxes[] = $box; if ($debug) { echo "Stop 1: box = \n"; $box->Dump(); echo '<br/>'; } } if (count($sizes) == 2) { $ratio = $sizes[0] / ($sizes[0] + $sizes[1]); if ($growWide) { $dim1 = $this->computeTableLayout($sizes[0], $boxAR * $ratio); $dim2 = $this->computeTableLayout($sizes[1], $boxAR * $ratio); $h = max($dim1[1], $dim2[1]); //echo "h=$h<br/>"; $dim2 = $this->computeTableLayoutGivenHeight($sizes[1], $h); $boxes[0] = new Rectangle($box->x, $box->y, $dim1[0], $h); $boxes[1] = new Rectangle($box->x + $dim1[0], $box->y, $dim2[0], $dim2[1]); } else { $dim1 = $this->computeTableLayout($sizes[0], $boxAR / $ratio); $dim2 = $this->computeTableLayout($sizes[1], $boxAR / (1 - $ratio)); $w = max($dim1[0], $dim2[0]); //echo "w=$w<br/>"; $dim2 = $this->computeTableLayoutGivenWidth($sizes[1], $w); $boxes[0] = new Rectangle($box->x, $box->y, $w, $dim1[1]); $boxes[1] = new Rectangle($box->x, $box->y + $dim1[1], $dim2[0], $dim2[1]); } if ($debug) { echo "Stop 2: box[0] = "; $boxes[0]->Dump(); echo '<br />'; echo " Stop 2: box[1] = "; $boxes[1]->Dump(); echo '<br/>'; echo 'Return ' . __LINE__ . ''; echo '<br/>'; } return $boxes; } // More than 2 $box2 = NULL; $r1 = NULL; $l1 = array(); $l2 = array(); $l3 = array(); // First compute R1 if ($pivotIndex > 0) { $l1 = array_slice($sizes, 0, $pivotIndex); $l1Size = $this->computeSize($l1); $b2Size = $this->computeSizeBetween($sizes, $pivotIndex, count($sizes) - 1); if ($growWide) { $dim1 = $this->computeTableLayoutGivenHeight($l1Size, $box->h); $dim2 = $this->computeTableLayoutGivenHeight($b2Size, $box->h); $r1 = new Rectangle($box->x, $box->y, $dim1[0], $dim1[1]); $box2 = new Rectangle($box->x + $dim1[0], $box->y, $dim2[0], $dim2[1]); } else { $dim1 = $this->computeTableLayoutGivenWidth($l1Size, $box->w); $dim2 = $this->computeTableLayoutGivenWidth($b2Size, $box->w); $r1 = new Rectangle($box->x, $box->y, $dim1[0], $dim1[1]); $box2 = new Rectangle($box->x, $box->y + $dim1[1], $dim2[0], $dim2[1]); } } else { $box2 = new Rectangle($box->x, $box->y, $box->w, $box->h); } // Recurse on R1 to compute better box2 if ($debug) { echo "<b>Recurse on R1 to get better box2</b><br />"; } if (count($l1) != 0) { if (count($l1) > 1) { $r1AR = $r1->aspectRatio(); if ($r1AR == 1) { $newGrowWidth = $growWide; } else { $newGrowWidth = $r1AR >= 1 ? true : false; } $l1boxes = $this->quantumLayoutParams($l1, $r1, $newGrowWide); } else { $l1boxes[0] = $r1; } $l1FinalBox = $this->computeUnion($l1boxes); if ($growWide) { $box2->h = $r1->h; } else { $box2->w = $r1->w; } /* echo "Final R1 box="; $l1FinalBox->Dump(); echo '<br />'; echo "box2="; $box2->Dump(); echo '<br />'; */ } // Display if ($debug) { $box->html(); $l1FinalBox->html('green', 'l1'); $box2->html(); } // Then compute R2 and R3 $box2AR = $box2->aspectRatio(); $first = true; $bestAR = 0.0; $bestdim1 = array(); $bestIndex = 0; for ($i = $pivotIndex + 1; $i < count($sizes); $i++) { $l2Size = $this->computeSizeBetween($sizes, $pivotIndex + 1, $i); $ratio = $pivotSize / ($pivotSize + $l2Size); if ($growWide) { $h1 = (int) ceil($ratio * $box2->h); $dim1 = $this->computeTableLayoutGivenHeight($pivotSize, $h1); } else { $w1 = (int) ceil($ratio * $box2->w); $dim1 = $this->computeTableLayoutGivenWidth($pivotSize, $w1); } $pivotAR = max($dim1[0] / $dim1[1], $dim1[1] / $dim1[0]); if ($first || $pivotAR < $bestAR) { $first = false; $bestAR = $pivotAR; $bestdim1 = $dim1; $bestl2Size = $l2Size; $bestIndex = $i; } } //echo "Best split: pivot=$pivotIndex, bestIndex=$bestIndex, bestAR=$bestAR, bestdim1=[" . $bestdim1[0] . ',' . $bestdim1[1] . "]<br />"; $l2 = array(); if ($bestIndex > 0) { $l2 = array_slice($sizes, $pivotIndex + 1, $bestIndex - $pivotIndex); $nl3 = count($sizes) - 1 - $bestIndex; if ($nl3 > 0) { $l3 = array_slice($sizes, $bestIndex + 1, $nl3); } } /* echo "\nSplit:\n"; echo "l1\n"; print_r($l1); echo "l2\n"; print_r($l2); echo "l3\n"; print_r($l3);*/ if (count($l2) > 0) { if ($growWide) { $dim2 = $this->computeTableLayoutGivenHeight($bestl2Size, $box2->h - $bestdim1[1]); $rp = new Rectangle($box2->x, $box2->y, $bestdim1[0], $bestdim1[1]); $r2 = new Rectangle($box2->x, $box2->y + $dim1[1], $dim2[0], $dim2[1]); if (count($l3) > 0) { $l3size = $this->computeSizeBetween($sizes, $bestIndex + 1, count($sizes) - 1); $dim3 = $this->computeTableLayoutGivenHeight($l3size, $box2->h); $r3 = new Rectangle($box2->x + $dim2[0], $box2->y, $dim3[0], $dim3[1]); } } else { $dim2 = $this->computeTableLayoutGivenWidth($bestl2Size, $box2->w - $bestdim1[0]); $rp = new Rectangle($box2->x, $box2->y, $bestdim1[0], $bestdim1[1]); $r2 = new Rectangle($box2->x + $dim1[0], $box2->y, $dim2[0], $dim2[1]); if (count($l3) > 0) { $l3size = $this->computeSizeBetween($sizes, $bestIndex + 1, count($sizes) - 1); $dim3 = $this->computeTableLayoutGivenWidth($l3size, $box2->w); $r3 = new Rectangle($box2->x, $box2->y + $dim2[1], $dim3[0], $dim3[1]); } } } else { if ($growWide) { $dim1 = $this->computeTableLayoutGivenHeight($pivotSize, $r1->h); } else { $dim1 = $this->computeTableLayoutGivenWidth($pivotSize, $r1->w); } $rp = new Rectangle($box2->x, $box2->y, $dim1[0], $dim1[1]); } /*echo "dim1\n";print_r($dim1); echo "dim2\n";print_r($dim2); echo "dim3\n";print_r($dim3);*/ //echo "rp = ";$rp->Dump(); echo "<br/>"; //echo "r2 = ";$r2->Dump(); echo "<br/>"; /* if ($r3) { echo "r3";$r3->Dump(); echo "<br/>"; } */ if ($debug) { echo "Draw<br/>"; $offset_y += 100; $rp->html("red", 'rp'); $r2->html("orange", 'r2'); if ($r3) { $r3->html("blue", 'r3'); } echo "r2="; $r2->Dump(); echo "<br/>"; } //------------------------------------------------------------------------------------------ // Finally, recurse on sublists in R2 and R3 if ($debug) { echo '<b>Recurse on sublists in R2 and R3</b><br />'; } if (count($l2) != 0) { if (count($l2) > 1) { if ($debug) { echo "recurse on R2<br/>"; } $r2AR = $r2->aspectRatio(); if ($debug) { echo "r2AR={$r2AR}<br/>"; } if ($r2AR == 1) { $newGrowWide = $growWide; } else { $newGrowWide = $r2AR >= 1 ? true : false; } $l2boxes = $this->quantumLayoutParams($l2, $r2, $newGrowWide); } else { $l2boxes[0] = $r2; } $l2FinalBox = $this->computeUnion($l2boxes); if ($debug) { echo "Final R2 box="; $l2FinalBox->Dump(); echo "<br/>"; } } if (count($l3) != 0) { if (count($l3) > 1) { if ($debug) { echo "<b>Recurse on R3</b><br />"; } $r3AR = $r3->aspectRatio(); if ($r3AR == 1) { $newGrowWide = $growWide; } else { $newGrowWide = $r3AR >= 1 ? true : false; } $l3boxes = $this->quantumLayoutParams($l3, $r3, $newGrowWide); } else { $l3boxes[0] = $r3; } $l3FinalBox = $this->computeUnion($l3boxes); if ($debug) { echo "Final R3 box="; $l3FinalBox->Dump(); echo "<br/>"; } } $rp_array = array(); $r3_array = array(); // empty $rp_array[] = $rp; /*echo "rp = "; $rp_array[0]->Dump(); echo "<br />"; echo "l1 = "; $l1FinalBox->Dump(); echo "<br />"; */ if ($debug) { echo '<b>Shift, expand/contract</b><br />'; } if (1) { // Shift and expand/contract the new layouts // depending on the the other sub-layouts if ($growWide) { if ($debug) { echo '<b>gw</b><br/>'; } if (count($l1) > 0) { $rp_array[0]->x = $l1FinalBox->x + $l1FinalBox->w; $rp_array[0]->y = $l1FinalBox->y; if ($debug) { echo "rp = "; $rp_array[0]->Dump(); echo "<br />"; } } if (count($l2) > 0) { if ($debug) { echo __LINE__ . '<br/>'; } $this->translateBoxesTo($l2boxes, $rp_array[0]->x, $rp_array[0]->y + $rp_array[0]->h); if ($debug) { foreach ($l2boxes as $l) { echo "l = "; $l->Dump(); echo "<br />"; } } $this->evenBoxWidth($rp_array, $l2boxes, $r3_array); if ($debug) { foreach ($l2boxes as $l) { echo "l = "; $l->Dump(); echo "<br />"; } } //exit(); if (count($l3) > 0) { $l2FinalBox = $this->computeUnion($l2boxes); if ($debug) { echo l2FinalBox; $l2FinalBox->Dump(); echo "<br />"; } if ($debug) { echo __LINE__ . '<br/>'; } $this->translateBoxesTo($l3boxes, $l2FinalBox->x + $l2FinalBox->w, $rp_array[0]->y); } $this->evenBoxHeight($l1boxes, $l2boxes, $l3boxes); } else { $this->evenBoxHeight($rp_array, $l1boxes, $r3_array); } } else { if ($debug) { echo '<b>no gw</b><br/>'; } if (count($l1) > 0) { $rp_array[0]->x = $l1FinalBox->x; $rp_array[0]->y = $l1FinalBox->y + $l1FinalBox->h; if ($debug) { echo "rp = "; $rp_array[0]->Dump(); echo "<br />"; } } if (count($l2) > 0) { //echo __LINE__. '<br/>'; //print_r($l2boxes); $this->translateBoxesTo($l2boxes, $rp_array[0]->x + $rp_array[0]->w, $rp_array[0]->y); if ($debug) { foreach ($l2boxes as $l) { echo "l = "; $l->Dump(); echo "<br />"; } } $this->evenBoxHeight($rp_array, $l2boxes, $r3_array); //print_r($rp_array); //print_r($l2boxes); if (count($l3) > 0) { $l2FinalBox = $this->computeUnion($l2boxes); if ($debug) { echo l2FinalBox; $l2FinalBox->Dump(); echo "<br />"; } if ($debug) { echo __LINE__ . '<br/>'; } $this->translateBoxesTo($l3boxes, $rp_array[0]->x, $l2FinalBox->y + $l2FinalBox->h); } $this->evenBoxWidth($l1boxes, $l2boxes, $l3boxes); } else { $this->evenBoxWidth($rp_array, $l1boxes, $r3_array); } } } if (count($l1) > 0) { $boxes = array_merge($boxes, $l1boxes); } $boxes[] = $rp_array[0]; if (count($l2) > 0) { $boxes = array_merge($boxes, $l2boxes); } if (count($l3) > 0) { $boxes = array_merge($boxes, $l3boxes); } $boxAR = $box->aspectRatio(); if ($boxAR == 1) { $newGrowWide = $growWide; } else { $newGrowWide = $boxAR >= 1 ? true : false; } if ($debug) { echo '<b>Boxes</b><br />'; foreach ($boxes as $b) { $b->Dump(); echo "<br/>"; } echo 'Return ' . __LINE__ . ''; } if ($debug) { echo '</li>'; echo '</ul>'; } return $boxes; }
public $length; public $width; function isSquare() { if ($this->length == $this->width) { return true; } else { return false; } } function getArea() { return $this->length * $this->width; } } $my_rectangle = new Rectangle(); $my_rectangle->length = $_GET["length"]; $my_rectangle->width = $_GET["width"]; ?> <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"> <title>Make a Rectangle</title> </head> <body> <div class="container"> <?php if ($my_rectangle->isSquare()) { echo "<h1>Congratulations! You made a square!</h1>"; } else {
} class Rectangle extends AbstractShape { public function draw() { print "Inside Rectangle::draw method.\n"; // 'print' works too } } $we = new Employee('Bob', 'Diamond'); echo $we->greeting(); echo PHP_EOL; $we->foo(); // overriding seems to work $he->foo(); $rect = new Rectangle(); // or 'new Rectangle()' $rect->draw(); echo empty(""); echo PHP_EOL; echo null == NULL; echo PHP_EOL; // PHP Arrays are in fact associative arrays, i.e. Map and Dictionaries $myArray = ["1" => "Hello", "2" => "How are you?", "3" => "Very well ty"]; $yourArray = []; echo $myArray["1"] . PHP_EOL; echo $myArray["2"] . PHP_EOL; echo $myArray["3"] . PHP_EOL; $myArray["4"] = "This is a new item"; echo $myArray["4"] . PHP_EOL; echo $myArray["4"] . PHP_EOL;
<?php require_once "rectangle.php"; require_once "square.php"; $rect1 = new Rectangle(9, 8); echo "The area of rectangle 1 is: " . $rect1->getArea() . PHP_EOL; $rect2 = new Rectangle(4, 5); echo "The area of rectangle 2 is: " . $rect2->getArea() . PHP_EOL; $square1 = new Square(5); echo "The perim of square 1 is: " . $square1->getPerimeter() . PHP_EOL; $square2 = new Square(4); echo "The perim of square 2 is: " . $square2->getPerimeter() . PHP_EOL;
public function getX4() { return $this->x4; } public function setY4($y4) { settype($y4, "integer"); $this->y4 = $y4; } public function getY4() { return $this->y4; } public function display() { parent::display(); // TODO: Change the autogenerated stub echo "x4=" . $this->x4 . "</br>"; echo "y4=" . $this->y4 . "</br>"; } } $rect = new Rectangle(); $rect->setX1(0); $rect->setY1(0); $rect->setX2(0); $rect->setY2(5); $rect->setX3(5); $rect->setY3(5); $rect->setX4(5); $rect->setY4(0); $rect->display();
/** * Вырезает произвольный квадрат из изображения. * Метод может принимать вырезаемый прямоугольник, обязанный быть квадратом, * либо параметры для создания такого прямоугольника. * * @throws IllegalArgumentException * @return AcImage */ public function cropSquare() { $a = func_get_args(); if (count($a) == 1 && $a[0] instanceof Rectangle && $a[0]->isSquare()) { $square = $a[0]; } else { if (count($a) == 2 && $a[0] instanceof Point && is_int($a[1])) { $square = new Rectangle($a[0], new Size($a[1], $a[1])); } else { if (count($a) == 3) { $square = new Rectangle(new Point($a[0], $a[1]), new Size($a[2], $a[2])); } else { throw new IllegalArgumentException(); } } } if (!$square->isInner(new Rectangle(new Point(0, 0), $this->getSize()))) { throw new IllegalArgumentException(); } return $this->crop($square); }
{ private $radius; public function __construct($x, $y, $radius) { parent::__construct($x, $y); $this->radius = $radius; } protected function drawShape() { return "Рисуем окружность с радиусом {$this->radius}"; } } class Rectangle extends Shape { private $width; private $height; public function __construct($x, $y, $width, $height) { parent::__construct($x, $y); $this->width = $width; $this->height = $height; } protected function drawShape() { return "Рисуем прямоугольник с шириной {$this->width} и высотой {$this->height}"; } } $circle = new Circle(0, 0, 50); $rectangle = new Rectangle(0, 0, 100, 50); $circle->draw(); $rectangle->draw();
public function __construct($width) { parent::__construct($width, $width); }
<?php require_once 'square.php'; $rectangle = new Rectangle(20, 40); echo 'The area of this rectangle is ' . $rectangle->area() . PHP_EOL; $square = new Square(20); echo 'The perimeter of this square is ' . $square->perimeter() . PHP_EOL;
public function resize($width, $height = null) { parent::resize($width, $width); // TODO: Change the autogenerated stub }
<?php require_once __DIR__ . "/../vendor/autoload.php"; require_once __DIR__ . "/../src/Rectangle.php"; $app = new Silex\Application(); $app->get("/", function () { return "Home"; }); $app->get("/new_rectangle", function () { return "<!DOCTYPE html>\n <html>\n <head>\n <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css'>\n <title>Make a rectangle!</title>\n </head>\n <body>\n <div class='container'>\n <h1>Geometry Checker</h1>\n <p>Enter the dimensions of your rectangle to see if it's a square.</p>\n <form action='/view_rectangle'>\n <div class='form-group'>\n <label for='length'>Enter the length:</label>\n <input id='length' name='length' class='form-control' type='number'>\n </div>\n <div class='form-group'>\n <label for='width'>Enter the width:</label>\n <input id='width' name='width' class='form-control' type='number'>\n </div>\n <button type='submit' class='btn-success'>Create</button>\n </form>\n </div>\n </body>\n </html>"; }); $app->get("/view_rectangle", function () { $my_rectangle = new Rectangle($_GET['length'], $_GET['width']); $area = $my_rectangle->getArea(); if ($my_rectangle->isSquare()) { return "<h1>Congratulations! You made a square! Its area is {$area}.</h1>\n <a href='/new_rectangle'>Back!</a>"; } else { return "<h1>Sorry! This isn't a square. Its area is {$area}.</h1>\n <a href='/new_rectangle'>Back!</a>"; } }); return $app;
<?php require_once "rectangle.php"; require_once "square.php"; $rect1 = new Rectangle(2, 7); echo $rect1->area(); echo "\n"; echo $rect1->perimeter(); echo "\n"; $rect2 = new Rectangle(3, 4); echo $rect2->area(); echo "\n"; echo $rect2->perimeter(); echo "\n"; $square1 = new Square(2); echo $square1->area(); echo "\n"; echo $square1->perimeter(); echo "\n"; $square1 = new Square(3); echo $square1->area(); echo "\n"; echo $square1->perimeter(); echo "\n"; ?> ?>
{ $this->base = $base; $this->height = $height; } # This might be different for each class of shape, because each Surface is calculated by a different formula ( St = b*h/2 and Sr = b*h) public abstract function surface(); } class Triangle extends Shape { # s = b*h/2 public function surface() { return round($this->base * $this->height / 2, 2); } } class Rectangle extends Shape { # s = b*h public function surface() { return round($this->base * $this->height, 2); } } $r = new Rectangle(); $r->getValue(15, 3); echo $r->surface() . "\n"; # echo 45 $t = new Triangle(); $t->getValue(15, 3); echo $t->surface() . "\n"; # echo 22.5
<?php include_once "autoload.php"; $a4 = new Rectangle(0.21, 0.297); $a4->perimetre(); $a4->surface(); echo $a4; echo '<pre>'; var_dump($a4); print_r($a4); echo '</pre>'; /* modif pour JB $truc = print_r($a4, true); //echo $truc; $posfleche = strripos($truc, "=>"); $posParent = strripos($truc, " )"); $lonLarg = trim(substr($truc, $posfleche+2, $posParent-2)); $tInfos = explode("/", $lonLarg); print_r($tInfos); */ echo 'modif de la longueur<br/>'; $a4->longueur = 3; $a4->perimetre(); $a4->surface(); echo $a4->largeur; echo $a4->longueur; // on demande une propriété qui // n'existe pas echo $a4->nawakNimporte; // on vérifie que largeur existe if (isset($a4->largeur)) { echo 'ok';
{ return $width * $height; } public function getPerimeter($width, $height) { return $width + $height; } public function isSquare() { return $this->width == $this->height; } } $width = 160; $height = 75; echo "<h2>With a width of {$width} and a height of {$height}...</h2>"; $r = new Rectangle($width, $height); echo '<p>The area of the rectangle is ' . $r->getArea($width, $height) . '</p>'; echo '<p>The perimeter of the rectangle is ' . $r->getPerimeter($width, $height) . '</p>'; echo '<p>This rectangle is '; if ($r->isSquare()) { echo 'also'; } else { echo 'not'; } echo ' a square.</p>'; ?> </p> </body> </html>
/** * Compute the caption area. */ private function computeCaptionArea() { $graphUnpaddedRight = $this->imageArea->x1 + ($this->imageArea->x2 - $this->imageArea->x1) * $this->graphCaptionRatio + $this->graphPadding->left + $this->graphPadding->right; $titleUnpaddedBottom = $this->imageArea->y1 + $this->titleHeight + $this->titlePadding->top + $this->titlePadding->bottom; $captionArea = new Rectangle($graphUnpaddedRight, $titleUnpaddedBottom, $this->imageArea->x2, $this->imageArea->y2); $this->captionArea = $captionArea->getPaddedRectangle($this->captionPadding); }