0

I created a file test.class.php containing the following code in a LAMP environment:

<?php

class MyClass 
{
    public var $variable;
};

$obj = new MyClass();

?>  

When I run:

php myclass.php

from the command line, I get the error:

Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /path/html/_dev/classes/test.class.php on line 5

If I access the same file via the browser, I get no errors. Any suggestions on what might be happening?

5 Answers 5

4

You do not need the var keywords ; you should use :

public $variable;

That's the correct PHP 5 syntax.


`var` is a reliquate of PHP 4, with which you'd have used :
var $variable;

But there was no public keyword in PHP 4 -- and public (PHP 5) and var (PHP 4) can't be used together.


For more informations about `public`, `var`, and other keywords of the same kind, see this page of the PHP manual : [**Visibility**][1]

As to why your code doesn't display any error when run by your webserver... Maybe a difference of configuration, that causes errors not to be displayed, in one case ?

Note : CLI and PHP/Apache can use different configuration files -- and often do.



As a sidenote (it causes no problem), you don't need the semi-colon at the end of the class' definition : your code should look like this :

class MyClass 
{
    public $variable;
}

$obj = new MyClass();
Sign up to request clarification or add additional context in comments.

Comments

0

Get rid of the var keyword, it's not necessary.

<?php

class MyClass 
{
    public $variable;
};

$obj = new MyClass();

?>

Comments

0

public var $variable; is too much. Just use one of those keywords.

Comments

0

Make sure that you're running php5's cli:

$ php -v
PHP 5.3.3 (cli) (built: Aug 22 2010 19:41:55) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

You can check which version of php you're using or where it's located with this command: which php.

And as the other guys said, get rid of this var. But it doesn't seem to be the problem. The error is reporting that private is an unknown token.

Good luck!

1 Comment

Thanks Gonzalo. Additionally, I found that my host has the PHP5 command available, which worked.
0

Figured it out. The PHP CLI version that the PHP command defaults to in my environment is PHP 4.4.8.

My host also has a PHP5 command set up to use PHP 5.2.14. When I executed the command

PHP5 myclass.php

Everything worked correctly. However, the code I posted originally produced another error. The

public var $variable 

line, which was a solution to another Stack Overflow post didn't work. I replaced it with

public $variable

and was able to test succesfully with that change.

This link helped out a lot http://www.yiiframework.com/forum/index.php?/topic/4319-error-on-yiibase-php-on-line-55/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.