moluv00
moluv00

Reputation: 374

Problem executing a class when using PHP CLI

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?

Upvotes: 0

Views: 559

Answers (5)

moluv00
moluv00

Reputation: 374

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/

Upvotes: 0

Gonzalo Larralde
Gonzalo Larralde

Reputation: 3541

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!

Upvotes: 0

Pascal MARTIN
Pascal MARTIN

Reputation: 401172

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();

Upvotes: 4

Shad
Shad

Reputation: 15471

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

Upvotes: 0

Mike Caron
Mike Caron

Reputation: 14561

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

<?php

class MyClass 
{
    public $variable;
};

$obj = new MyClass();

?>

Upvotes: 0

Related Questions