Jump to content

Catchable fatal error: Argument 1 passed to ... must be an array, object


grahamsimmons

Recommended Posts

I'm a newbie, but I've managed to write some PHP code to handle a shopping cart. Running the code locally in PhpED is works great, but on a server a get the error ...

 

Catchable fatal error: Argument 1 passed to ShoppingCart::initializeCart() must be an array, object given, called in includes/common.inc on line 20 and defined in includes/class_cart.inc on line 20.

 

Below is my code and I've highlighted both offending line 20's in red and lines of importance in green. Any ideas why I get this error? As far as I can see, an array is being passed into the function and I can see that in debug mode locally  :'(

 

Index.php code

<?php

    session_start();

    include('includes/common.inc');

    ....

?>

 

common.inc

<?php

   

    //Initialize session cart

    if(!isset($_SESSION['cart']) || !Is_Array($_SESSION['cart'])) {$_SESSION['cart'] = array();}

   

    include("includes/class_settings.inc");

    include("includes/conn.inc");

    include("includes/class_admin.inc"); 

    include("includes/class_product.inc");

    include("includes/class_cart.inc");

   

    $objDB = new SiteSettings();

    $admin = new SiteAdmin();

    $product = new SiteProduct();

    $cart = new ShoppingCart();

    $cart->initializeCart($_SESSION['cart']);

    ...

?>

 

class_cart.inc

function initializeCart(array &$myCart){

    $this->myCart =& $myCart;

}

 

Try var_dump'ing it justbefore you call the method.

 

I've done some dumping  ;)  Offending lines are in red and lines in green are what I've just added. Looks like the session cart is an array until the class constructor runs! I'm guessing now  :-[  Why does it not stay as an array since nothing is touching it?

 

common.inc

<?php

   

    //Initialize cart

    if(!isset($_SESSION['cart']) || !Is_Array($_SESSION['cart'])){ $_SESSION['cart'] = array(); }

    var_dump($_SESSION['cart']);

    echo "<hr>";

    //======================================================================

    // Include required files.

    //======================================================================

   

    include("includes/class_settings.inc");

    include("includes/conn.inc");

    include("includes/class_admin.inc"); 

    include("includes/class_product.inc");

    include("includes/class_cart.inc");

   

    $objDB = new SiteSettings();

    $admin = new SiteAdmin();

    $product = new SiteProduct();

    $cart = new ShoppingCart();

    var_dump($_SESSION['cart']);

    $cart->initializeCart($_SESSION['cart']);    ...

?>

 

class_cart.inc

Class ShoppingCart {

   

    protected $myCart;

    protected $cookieName;

    protected $cookieExpire;

    protected $cookieExpireDays;

 

    //Class constructor.

    function __Construct($name = "oasSC", $days = 1){

        $myCart = array();

        $cookieName = $name;

        $cookieExpireDays = $days;

        $cookieExpire = ($cookieExpireDays * 86400); //Convert days to seconds

    }

 

    function initializeCart(array &$myCart){

        $this->myCart =& $myCart;

    }

}

 

The result is now ...

 

array(0) { } 

 


 

  object(ShoppingCart)#4 (4) { ["myCart:protected"]=> NULL ["cookieName:protected"]=> NULL ["cookieExpire:protected"]=> NULL ["cookieExpireDays:protected"]=> NULL }

Catchable fatal error: Argument 1 passed to ShoppingCart::initializeCart() must be an array, object given, called in /home/grahamsi/public_html/oas/includes/common.inc on line 22 and defined in /home/grahamsi/public_html/oas/includes/class_cart.inc on line 20

 

Possibly one of the files you are including, or one of the constructors for the objects you instantiate are changing it. That is the danger of storing data globally in a program. I would make a local copy by doing something like $cart = $_SESSION['cart'] and then use that instead.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.