Jump to content
Larry Ullman's Book Forums

Recommended Posts

I've been banging on the following error for awhile and I cannot tell what is wrong, please help:

 

Catchable fatal error: Argument 2 passed to ShapeFactory::Create() must be an array, string given, called in /home/content/14/11625314/html/AdvPHPwOOP/factory.php on line 22 and defined in /home/content/14/11625314/html/AdvPHPwOOP/ShapeFactory.php on line 11

 

 

I'm using PHP 5.3 thru goDaddy

 

Here's the code:

 

<!doctype html>

<html lang="en">
<head>
    <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>
 
 
****************
 
<?php # Script 7.3 - ShapeFactory.php
// This page defines a ShapeFactory class which uses the Factory pattern.
 
/* The ShapeFactory class.
 * The class contains no attributes.
 * The class contains one method: Create().
 */
abstract class ShapeFactory {
    
    // Static method that creates objects:
    static function Create($type, array $sizes) {
        
        // Determine the object type based upon the parameters received.
        switch ($type) {
            case 'rectangle':
                return new Rectangle($sizes[0], $sizes[1]);
                break;
            case 'triangle':
                return new Triangle($sizes[0], $sizes[1], $sizes[2]);
                break;
        } // End of switch.
         
    } // End of Create() method.
 
} // End of ShapeFactory class.
 
Link to comment
Share on other sites

 Share

×
×
  • Create New...