RonInNewYork Posted April 29, 2011 Share Posted April 29, 2011 I'm reading through this book which is actually not bad but, on page 286 there's a shopping_cart.php code (which I downloaded from the web site). The code that seems relevant to me is this: <?php $totalPrice = 0; foreach ( $_SESSION["cart"] as $product) { $totalPrice += $product->getPrice(); //<<<LINE 94 ?> The error I get is Fatal error: Call to a member function getPrice() on a non-object in C:\usr\web\shopping_cart.php on line 94 Should $product be a cast to the Product object? class Product { private $productId; private $productName; private $price; public function __construct( $productId, $productName, $price ) { $this->productId = $productId; $this->productName = $productName; $this->price = $price; } public function getPrice() { return $this->price; } //etc. } Quote Link to comment https://forums.phpfreaks.com/topic/235114-beginning-php-53-problem/ Share on other sites More sharing options...
fugix Posted April 29, 2011 Share Posted April 29, 2011 welcome to the site.. when you are inserting code, please use the code tags as this makes the code easily distinguishable. Quote Link to comment https://forums.phpfreaks.com/topic/235114-beginning-php-53-problem/#findComment-1208331 Share on other sites More sharing options...
wildteen88 Posted April 29, 2011 Share Posted April 29, 2011 I'm reading through this book which is actually not bad but, on page 286 there's a shopping_cart.php code (which I downloaded from the web site). The code that seems relevant to me is this: <?php $totalPrice = 0; foreach ( $_SESSION["cart"] as $product) { $totalPrice += $product->getPrice(); //<<<LINE 94 ?> The error I get is Fatal error: Call to a member function getPrice() on a non-object in C:\usr\web\shopping_cart.php on line 94 Should $product be a cast to the Product object? Yes as long as you have initiated the object $product = new Product; Quote Link to comment https://forums.phpfreaks.com/topic/235114-beginning-php-53-problem/#findComment-1208350 Share on other sites More sharing options...
RonInNewYork Posted April 29, 2011 Author Share Posted April 29, 2011 But shouldn't the $product object already hold all the values? Product has only one constructor taking 3 parameters. Shouldn't the foreach code handle this? How would you write it? I've tried casting $product to Product but that didn't work (Product)$product. RON Quote Link to comment https://forums.phpfreaks.com/topic/235114-beginning-php-53-problem/#findComment-1208365 Share on other sites More sharing options...
wildteen88 Posted April 29, 2011 Share Posted April 29, 2011 Where are you calling $product->getPrice()? Is it in the same file as when you initiated the product object (when you created the product). Or in a different page? If its in a different page then variables you define in one page will not be passed on to the next. Variables die when the script finishes. Quote Link to comment https://forums.phpfreaks.com/topic/235114-beginning-php-53-problem/#findComment-1208376 Share on other sites More sharing options...
RonInNewYork Posted April 29, 2011 Author Share Posted April 29, 2011 Here' s the entire code. It's just from a book as I'm learning PHP (after years in Java and C#/VB.NET): <?php session_start(); class Product { private $productId; private $productName; private $price; public function __construct( $productId, $productName, $price ) { $this->productId = $productId; $this->productName = $productName; $this->$price = $price; } public function getId() { return $this->productId; } public function getName() { return $this->productName; } public function getPrice() { return $this->price; } } $products = array( 1 => new Product( 1, "SuperWidget", 19.99 ), 2 => new Product( 2, "MegaWidget", 29.99 ), 3 => new Product( 3, "WonderWidget", 39.99 ) ); if ( !isset( $_SESSION["cart"] ) ) $_SESSION["cart"] = array(); if ( isset( $_GET["action"] ) and $_GET["action"] == "addItem" ) { addItem(); } elseif ( isset( $_GET["action"] ) and $_GET["action"] == "removeItem" ) { removeItem(); } else { displayCart(); } function addItem() { global $products; if ( isset( $_GET["productId"] ) and $_GET["productId"] >= 1 and $_GET["productId"] <= 3 ) { $productId = (int) $_GET["productId"]; if ( !isset( $_SESSION["cart"][$productId] ) ) { $_SESSION["cart"][$productId] = $products[$productId]; } } session_write_close(); header( "Location: shopping_cart.php" ); } function removeItem() { global $products; if ( isset( $_GET["productId"] ) and $_GET["productId"] >= 1 and $_GET["productId"] <= 3 ) { $productId = (int) $_GET["productId"]; if ( isset( $_SESSION["cart"][$productId] ) ) { unset( $_SESSION["cart"][$productId] ); } } session_write_close(); header( "Location: shopping_cart.php" ); } function displayCart() { global $products; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>A shopping cart using sessions</title> <link rel="stylesheet" type="text/css" href="common.css" /> </head> <body> <h1>Your shopping cart</h1> <dl> <?php $totalPrice = 0; foreach ( $_SESSION["cart"] as $product) { $totalPrice += $product->getPrice(); //<<<THIS IS WHERE THE CODE ABENDS ?> <dt><?php echo $product->getName() ?></dt> <dd>$<?php echo number_format( $product->getPrice(), 2 ) ?> <a href="shopping_cart.php?action=removeItem&productId=<?php echo $product->getId() ?>">Remove</a></dd> <?php } ?> <dt>Cart Total:</dt> <dd><strong>$<?php echo number_format( $totalPrice, 2 ) ?></strong></dd> </dl> ?> <h1>Product list</h1> <dl> <?php foreach ( $products as $product ) { ?> <dt><?php echo $product->getName() ?></dt> <dd>$<?php echo number_format( $product->getPrice(), 2 ) ?> <a href="shopping_cart.php?action=addItem&productId=<?php echo $product->getId() ?>">Add Item</a></dd> <?php } ?> </dl> <?php } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/235114-beginning-php-53-problem/#findComment-1208397 Share on other sites More sharing options...
markjoe Posted April 29, 2011 Share Posted April 29, 2011 I think objects need to be serialized to be stored in the $_SESSION array. Then of course unserialized when retrieved. Quote Link to comment https://forums.phpfreaks.com/topic/235114-beginning-php-53-problem/#findComment-1208412 Share on other sites More sharing options...
RonInNewYork Posted April 30, 2011 Author Share Posted April 30, 2011 I'm not sure how to implement that. I can see the problem is getting at the Product objects in the $product array. When I put if (isset($_SESSION["cart"] { before the first foreach, then that code doesn't error out. HOWEVER, the second foreach does error h1>Product List</h1> <dl> <?php foreach ($products as $product) {?> <dt><?php echo $product->$getName() ?></dt> <dd>$<?php echo number_format($product->getPrice(),2) ?> <a href="shopping_cart.php?action=addItem&productId<=<?php echo $product->getId() ?>">Add Item</a></dd> <?php } ?> </dl> errors out with "Undefined variable: getName in...line" and "Method name must be a string." Thanks for your help. RON Quote Link to comment https://forums.phpfreaks.com/topic/235114-beginning-php-53-problem/#findComment-1208721 Share on other sites More sharing options...
wildteen88 Posted April 30, 2011 Share Posted April 30, 2011 $product->$getName() should be $product->getName() Quote Link to comment https://forums.phpfreaks.com/topic/235114-beginning-php-53-problem/#findComment-1208724 Share on other sites More sharing options...
RonInNewYork Posted April 30, 2011 Author Share Posted April 30, 2011 Thanks that was sloppy of me. But now I get: Call to undefined method Product::getName()... If it isn't one thing, it's another, right? APOLOGIES: A couple of spelling and other typos. BUT, even when I fixed them the code seems incredibly broken. It will run with no errors, for instance $_SESSION["cart"] is always unset. Quote Link to comment https://forums.phpfreaks.com/topic/235114-beginning-php-53-problem/#findComment-1208739 Share on other sites More sharing options...
darkfreaks Posted April 30, 2011 Share Posted April 30, 2011 have you tried using something like http://us2.php.net/manual/en/function.session-set-save-handler.php to store your sessions Quote Link to comment https://forums.phpfreaks.com/topic/235114-beginning-php-53-problem/#findComment-1208749 Share on other sites More sharing options...
RonInNewYork Posted April 30, 2011 Author Share Posted April 30, 2011 Thanks. That is interesting as well, but I think this was an accretion of typos. It now works (more or less). Quote Link to comment https://forums.phpfreaks.com/topic/235114-beginning-php-53-problem/#findComment-1208750 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.