PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
tommy1987
Forum Commoner
Posts: 92 Joined: Tue Feb 21, 2006 8:35 pm
Post
by tommy1987 » Mon Apr 14, 2008 9:02 am
Hello,
I am trying to use an online tutorial with the following code:
Code: Select all
<?php
class Shopping_Cart {
private $cart;
function __construct($cart="") {
$this->cart = $cart;
}
function getCart() {
return $this->getCart();
}
function addToCart($item) {
if(isset($this->cart[$item])) {
$this->cart[$item]++;
} else {
$this->cart[$item] = 1;
}
}
function deleteFromCart($item) {
if(isset($this->cart[$item])) {
$this->cart[$item]--;
if($this->cart[$item] == 0) {
unset($this->cart[$item]);
}
}
}
}
?>
This produces the following error when I try to utilize this class in my program:
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in Shopping_Cart.php on line 3.
Any ideas please?
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Mon Apr 14, 2008 9:12 am
The tutorial is for PHP 5 and you're using PHP 4.
tommy1987
Forum Commoner
Posts: 92 Joined: Tue Feb 21, 2006 8:35 pm
Post
by tommy1987 » Mon Apr 14, 2008 1:17 pm
Upgraded to PHP 5 and still the problem occurs, hence that is not the problem. I appreciate the help though and will continue to work at it. If anyone else has any ideas then please let me know.
Tom
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Mon Apr 14, 2008 1:19 pm
There are no PHP5 errors in the code you posted. Try running a phpinfo() in another file to see what version is actually being run.
tommy1987
Forum Commoner
Posts: 92 Joined: Tue Feb 21, 2006 8:35 pm
Post
by tommy1987 » Mon Apr 14, 2008 1:38 pm
Now it just displays a 500 Internal Server Error since the upgrade to PHP 5. I can confirm that it definitely is PHP 5 since I ran phpinfo().
My code is this:
Code: Select all
<html>
<head></head>
<body>
<?php
include("Shopping_Cart.php");
$cart = new Shopping_Cart();
$cart->addToCart(1);
$cart->addToCart(3);
$cart->addToCart(3);
print_r($cart->getCart());
$cart->deleteFromCart(1);
print_r($cart->getCart());
?>
</body>
</html>
Ideas?
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Mon Apr 14, 2008 1:47 pm
An internal server error implies something is wrong with the set up of Apache or PHP rather than the script. Do other scripts work?
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Mon Apr 14, 2008 5:46 pm
onion2k wrote: An internal server error implies something is wrong with the set up of Apache or PHP rather than the script. Do other scripts work?
Or sometimes because you have a recursive include() or function call.
tommy1987
Forum Commoner
Posts: 92 Joined: Tue Feb 21, 2006 8:35 pm
Post
by tommy1987 » Tue Apr 15, 2008 2:20 am
Here is the complete code, PHP 5 seems to be working well with a few other scripts. It is just this one that is causing me grief. I have given the complete code because it is not a lot at all.
Test Class:
Code: Select all
<html>
<head></head>
<body>
<?php
include 'Shopping_Cart.php';
$cart = new Shopping_Cart();
$cart->addToCart(1);
$cart->addToCart(3);
$cart->addToCart(3);
print_r($cart->getCart());
$cart->deleteFromCart(1);
print_r($cart->getCart());
?>
</body>
</html>
Shopping_Cart:
Code: Select all
<?php
class Shopping_Cart {
private $cart;
function __construct($cart="") {
$this->cart = $cart;
}
function getCart() {
return $this->getCart();
}
function addToCart($item) {
if(isset($this->cart[$item])) {
$this->cart[$item]++;
} else {
$this->cart[$item] = 1;
}
}
function deleteFromCart($item) {
if(isset($this->cart[$item])) {
$this->cart[$item]--;
if($this->cart[$item] == 0) {
unset($this->cart[$item]);
}
}
}
}
?>
Appreciate the help.
EverLearning
Forum Contributor
Posts: 282 Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia
Post
by EverLearning » Tue Apr 15, 2008 4:13 am
Chris Corbyn wrote: Or sometimes because you have a recursive include() or function call.
Chris is right. You have an recursive function call. In Shopping_Cart class
getCart() method calls itself infinitely, and thats why you're getting internal sever error.
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Tue Apr 15, 2008 4:21 am
I think getCart() is supposed to return $this->cart instead
tommy1987
Forum Commoner
Posts: 92 Joined: Tue Feb 21, 2006 8:35 pm
Post
by tommy1987 » Tue Apr 15, 2008 4:39 am
Thanks very much, issue resolved.