A simple class code help
Results 1 to 13 of 13

Thread: A simple class code help

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    6

    A simple class code help

    I have just started learning PHP and i was asked to try the code for CLASSES AND OBJECT given below, But when i try i get error message given below the posted code here. Please help me find out what is wrong...
    PHP Code:
    <html>
    <head>
    <title>opensoftworkshop.com</title>
    </head>
    <body>
    <?php
    class book
    {
    function 
    Bookdetails()
    {
    echo 
    "Book Name: Software Engineering<br>";
    echo 
    "Author: IAN SOMMERVILLE<br>";
    }
    }
    $a = new book();
    $a-> Bookdetails();
    ?>
    </body>
    </html>
    ERROR: Parse error: syntax error, unexpected '{', expecting '(' in C:\Users\user\xampp\htdocs\test\class.php on line 10
    Last edited by bradgrafelman; 04-19-2012 at 12:45 PM. Reason: PHP bbcode tags added

  2. #2
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,410
    Welcome to PHPBuilder! When posting PHP code, please use the board's [php]..[/php] bbcode tags as they make your code much easier to read and analyze.

    As for your issue... there are no syntax errors in the code snippet you posted above. Are you sure that is the exact code you're using when you get that error message? Also, which version of PHP are you using?

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    6

    version

    version is PHP/5.3.8

    When i run this in http://localhost/test/class.php

    the same error asParse error: syntax error, unexpected '{', expecting '(' in C:\Users\user\xampp\htdocs\test\class.php on line 10

    shows up...

  4. #4
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,410
    Are you sure the above code snippet is the exact same code you're using? Like I said, the code snippet you posted above contains no syntax errors and will parse just fine. And since you're not using some archaic version of PHP, I can almost guarantee that something is different between the code you posted and the code you're executing.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    yes same code i just copied and pasted... anyway thanks i think i must take this to my tutor tomorrow... and find out.

  6. #6
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,410
    Just to make sure we're looking at the same thing... could you try renaming your 'class.php' file to have a .txt extension (e.g. 'class.php.txt') and attach it to your next reply?

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    6

    thks for the support

    Thnks for your efforts i have attached the txt file
    Attached Files Attached Files

  8. #8
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,410
    Sorry... I'm afraid I'm out of ideas. I downloaded the .txt file attached to your post and still received no parse errors.

    EDIT: The only other thing I can think of is perhaps it's some sort of browser cache issue? Have you tried executing that PHP script from the command line?

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    i appreciate your efforts thanks

  10. #10
    newbie php developer Damian_'s Avatar
    Join Date
    Jun 2011
    Location
    london
    Posts
    93
    maybe this???

    <html>
    <head>
    <title>opensoftworkshop.com</title>
    </head>
    <body>
    <?php
    class book
    {

    public $var = 'Book Name: Software Engineering';

    public function Bookdetails()
    {
    echo $this->var;
    }
    }

    $a = new book();
    $a-> Bookdetails();
    ?>
    </body>
    </html>
    [/php]

  11. #11
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Quote Originally Posted by newprogramer View Post
    i have attached the txt file
    copied,pasted,run, ...works. (php5.3.8)

    try running only the php code?

  12. #12
    Junior Member
    Join Date
    Apr 2012
    Posts
    6

    Talking it worked

    Thank you so much it works...

    My mistake actually i was not running this from localhost, i had been trying to open this from ...

    C:\Users\user\xampp\htdocs\test\class.php

    I am loving this php challenges, thank u all so much both these codes work now,,,
    Quote Originally Posted by Damian_ View Post
    maybe this???

    <html>
    <head>
    <title>opensoftworkshop.com</title>
    </head>
    <body>
    <?php
    class book
    {

    public $var = 'Book Name: Software Engineering';

    public function Bookdetails()
    {
    echo $this->var;
    }
    }

    $a = new book();
    $a-> Bookdetails();
    ?>
    </body>
    </html>
    [/php]

  13. #13
    newbie php developer Damian_'s Avatar
    Join Date
    Jun 2011
    Location
    london
    Posts
    93

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •