tags:

views:

136

answers:

3

I'm getting this error whenever I attempt to declare a class:

Parse error: syntax error, unexpected T_VARIABLE, expecting T_OLD_FUNCTION or
T_FUNCTION or T_VAR or '}' in /home3/foundloc/public_html/booka/page2.php
on line 7 (line 7 is the class declaration by the way).

Here's the incredibly simple class I am attempting to declare:

Class abc
{

$a = “Hello!”;

} 

Is there some setting on PHP that i need to turn on? I feel like this is one of those 'did you check if the TV is plugged in' type of issues....

+4  A: 

Try

class abc {
  public $a = "Hello!";
} 

or

class abc {
  var $a = "Hello!";
} 
Kamil Szot
Class names should start with a capital letter. Also since PHP 5, either `private`, `protected` or `public` should be used for class members.
Felix Kling
Ok for some reason, your first reply with 'public' results in the same error. The 2nd reply with 'var' works. whats going on here?
Phil
@Phil: Which PHP version are you using? Looks like you are using PHP < 5.
Felix Kling
supposedly 5.2.11.I can do what i need to do with arrays and globals, but thought this would be a good time to start learning some oop stuff...ack...
Phil
Can you post your entire code snippet? Something seems fishy.
Anthony Forloney
Page 1:<?phprequire ('page2.php');$hello = new abc(); ?>Page 2: <?phpclass abc { private $a = "Hello!";} ?> Error:Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home3/foundloc/public_html/booka/page2.php on line 3
Phil
+1  A: 

try

<?php
Class abc {
   var $a = "Hello!";
}
?>

Should work. You have to state the visibility of the member with var or public or private combined with static keyword.

Should find more info in the php man page describing the properties (php terminology for member)

Eineki
+1  A: 

You can't declare properties in classes like that. Members of a class can be either data members (constants and properties) or methods. In the PHP 5 way of doing things, this is basically how it works:

// de facto best practice: class names start with uppercase letter
class Abc
{
    // de facto best practice: ALL UPPERCASE letters for constants
    const SOME_COSTANT = 'this value is immutable'; // accessible outside and inside this class like Abc::SOME_CONSTANT or inside this class like self::SOME_CONSTANT

    public $a = 'Hello'; // a data member that is accessible to all
    protected $b = 'Hi'; // a data membet that is accessible to this class, and classes that extend this class
    private $c = 'Howdy'; // a data member that is accessible only to this class

    // visibility keywords apply here also
    public function aMethod( $with, $some, $parameters ) // a method
    {
        /* do something */
    }
}

You should really not consider using the php 4 practice of declaring data members with the var keyword, unless you are still developing for php 4 of course.

fireeyedboy
i copied and pasted your text word for word in a new document by itself with <? ?> around it and received this error:Parse error: syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home3/foundloc/public_html/booka/page2.php on line 5
Phil
Really?! I just copied the exact same code, and it worked fine. Version 5.2.9 here. What version of php are you using? To find out, do a simple `<?php phpinfo(); ?>`
fireeyedboy
ahhh hah....cpanel claims i'm running 5.2.11....but php_info says PHP Version 4.4.9. would that do it?
Phil
Aahh, no, you need PHP 5 to use this syntax. If at all possible, I would advice you to use PHP 5. Not sure why the version in cpanel differs from phpinfo() though. Assuming your on a webhost, ask them to activate php 5 for your account, and/or have them figure out why there is a discrepancy.
fireeyedboy