-> operator giving parse error

Discussion in 'PHP' started by madscout2000, Apr 14, 2007.

  1. #1
    I am learning OOP in PHP and am going through the tutorial in my "Beginning PHP5" book. I entered the following two files verbatim, but get the error message

    "Parse error: parse error, unexpected T_VARIABLE in /home/nanoserv/public_html/testdemo.php on line 5"

    class.Demo.php
    testdemo.php
     
    madscout2000, Apr 14, 2007 IP
  2. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #2
    i'm not an expert in OPP But i see a ";" missing .

    $objAnotherDemo = new Demo() 
    $objAnotherDemo->name = "Ed";
    PHP:

    $objAnotherDemo = new Demo() ;
     
    commandos, Apr 14, 2007 IP
  3. madscout2000

    madscout2000 Peon

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I always do that - I always miss some mundane detail!

    Ok, semicolons fixed, now I get an error for the line that says:

    "Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/nanoserv/public_html/class.Demo.php on line 4"

    Thanks for your help!
     
    madscout2000, Apr 14, 2007 IP
  4. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sounds like you're running PHP4 but trying to use PHP5 syntax.

    Upgrade your version of PHP.
     
    rodney88, Apr 14, 2007 IP
  5. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #5
    are you sure you are running php5 ? because i think your problem is that the class is specifying variable and such as "public" or "protected".

    From what I've heard, PHP doesn't support public/private members of a class.

    Maybe it does in PHP5, but if you're running PHP4, that's your problem

    make a file , write in it :

    <?phpinfo();?>
    PHP:
    and see what php version you are using .
     
    commandos, Apr 14, 2007 IP
  6. madscout2000

    madscout2000 Peon

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This is the case. I can't upgrade the version of PHP because my server owner prefers PHP4. (looks like I bought the wrong book!) What is the correct PHP4 syntax?

    Thanks!
     
    madscout2000, Apr 14, 2007 IP
  7. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #7
    put var instead of public
     
    commandos, Apr 14, 2007 IP
  8. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I'm not sure how your host can prefer using an outdated version - for the most part, it's backwards compatible with PHP4 anyway. I recommend pushing your host a bit harder or moving to another.

    There's really not much point becoming familiar with the PHP4 OOP simply because your host is too lazy to upgrade. The object model in PHP5 is a significant improvement on PHP4 - it's not that there's a different syntax, it just doesn't support a whole lot of useful features of the newer version. In this instance, you can't specify public, private and protected members (or methods for that matter) and all are treated as public. For PHP4 and below, you need to use var $name.
     
    rodney88, Apr 14, 2007 IP
  9. madscout2000

    madscout2000 Peon

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for the reply. I will talk to my host!
     
    madscout2000, Apr 14, 2007 IP