Page 1 of 1

Class Member Variable Syntax Error

Posted: Mon Apr 14, 2008 9:02 am
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?

Re: Class Member Variable Syntax Error

Posted: Mon Apr 14, 2008 9:12 am
by onion2k
The tutorial is for PHP 5 and you're using PHP 4.

Re: Class Member Variable Syntax Error

Posted: Mon Apr 14, 2008 1:17 pm
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

Re: Class Member Variable Syntax Error

Posted: Mon Apr 14, 2008 1:19 pm
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.

Re: Class Member Variable Syntax Error

Posted: Mon Apr 14, 2008 1:38 pm
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?

Re: Class Member Variable Syntax Error

Posted: Mon Apr 14, 2008 1:47 pm
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?

Re: Class Member Variable Syntax Error

Posted: Mon Apr 14, 2008 5:46 pm
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.

Re: Class Member Variable Syntax Error

Posted: Tue Apr 15, 2008 2:20 am
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.

Re: Class Member Variable Syntax Error

Posted: Tue Apr 15, 2008 4:13 am
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.

Re: Class Member Variable Syntax Error

Posted: Tue Apr 15, 2008 4:21 am
by Chris Corbyn
I think getCart() is supposed to return $this->cart instead :)

Re: Class Member Variable Syntax Error

Posted: Tue Apr 15, 2008 4:39 am
by tommy1987
Thanks very much, issue resolved.