Jump to content
Larry Ullman's Book Forums
Sign in to follow this  

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.
 

Share this post


Link to post
Share on other sites

I can't tell for certain, but I assume the problem has to do with the definition of the "dimensions" inputs in your form.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...