Class Member Variable Syntax Error

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

Post Reply
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Class Member Variable Syntax Error

Post by tommy1987 »

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?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Class Member Variable Syntax Error

Post by onion2k »

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

Re: Class Member Variable Syntax Error

Post by tommy1987 »

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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Class Member Variable Syntax Error

Post by John Cartwright »

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

Re: Class Member Variable Syntax Error

Post by tommy1987 »

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?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Class Member Variable Syntax Error

Post by onion2k »

An internal server error implies something is wrong with the set up of Apache or PHP rather than the script. Do other scripts work?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Class Member Variable Syntax Error

Post by Chris Corbyn »

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

Re: Class Member Variable Syntax Error

Post by tommy1987 »

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.
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Class Member Variable Syntax Error

Post by EverLearning »

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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Class Member Variable Syntax Error

Post by Chris Corbyn »

I think getCart() is supposed to return $this->cart instead :)
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Re: Class Member Variable Syntax Error

Post by tommy1987 »

Thanks very much, issue resolved.
Post Reply