<meta charset="utf-8"> <title>Factory</title> <link rel="stylesheet" href="style.css"> </head> <body> <?php # Script 7.4 - factory.php // This page uses the ShapeFactory class (Script 7.2). // Load the class definitions: require 'ShapeFactory.php'; require 'Shape.php'; require 'Triangle.php'; require 'Rectangle.php'; // Minimal validation: if (isset($_GET['shape'], $_GET['dimensions'])) { // Create the new object: $obj = ShapeFactory::Create($_GET['shape'], $_GET['dimensions']); // Print a little introduction: echo "<h2>Creating a {$_GET['shape']}...</h2>"; // Print the area: echo '<p>The area is ' . $obj->getArea() . '</p>'; // Print the perimeter: echo '<p>The perimeter is ' . $obj->getPerimeter() . '</p>'; } else { echo '<p class="error">Please provide a shape type and size.</p>'; } // Delete the object: unset($obj); ?> </body> </html>
This will give the result of area = 168 and perimeter = 52 --------------------------------------- Triangle Test: factory.php?shape=triangle&dimensions[]=12&dimensions[]=14&dimensions[]=5 This will give the result of area = 29.230762904858 and perimeter = 31 --------------------------------------- Shapes not exist Test: factory.php?shape=tfriangle&dimensions[]=12&dimensions[]=14&dimensions[]=5 --> <?php require 'ShapeFactory.php'; require 'Shape.php'; require 'Rectangle.php'; require 'Triangle.php'; //perform minimal validation if (isset($_GET['shape'], $_GET['dimensions'])) { //create the object $shape = $_GET['shape']; $dimensions = $_GET['dimensions']; $obj = ShapeFactory::Create($shape, $dimensions); echo "<h2> Creating a {$shape} ...</h2>"; /* As $obj is now some sort of Shape-based object, you know it has a getArea() method that can be called. */ echo "<p>The area is " . $obj->getArea() . "</p>"; echo "<p>The parameter is " . $obj->getPerimeter() . "</p>"; } else { echo "<p>Please provide a shape type and size.</p>"; } unset($obj, $shape, $dimensions); ?> </body> </html>
spl_autoload_register(function ($class) { require $class . '.php'; }); // Minimal validation: if (isset($_GET['submit'])) { if (isset($_GET['shape'], $_GET['dim'])) { $dim = trim($_GET['dim']); if (empty($dim)) { die('<p class="error">Please provide dimensions in correct format.</p>'); } $dim = explode(',', $dim); var_dump($dim); $dim = array_map('intval', $dim); var_dump($dim); // Create the new object: $obj = ShapeFactory::Create($_GET['shape'], $dim); // Print a little introduction: echo "<h2>Creating a {$_GET['shape']}...</h2>"; // Print the area: echo '<p>The area is ' . $obj->getArea() . '</p>'; // Print the perimeter: echo '<p>The perimeter is ' . $obj->getPerimeter() . '</p>'; } else { echo '<p class="error">Please provide a shape type and size.</p>'; } // Delete the object: unset($obj); } ?> <form action="<?php echo $_SERVER['PHP_SELF'];
<?php abstract class ShapeFactory { static function Create($type, array $size) { switch ($type) { case 'rect': return new rect($size[0], $size[1]); break; case 'tri': return new tri($size[0], $size[1], $size[2]); break; } } } $dimension = array(); $dimension[] = 1; $dimension[] = 2; $obj = ShapeFactory::Create('tri', $dimension);