Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   class errors (http://www.programmingforums.org/showthread.php?t=11681)

kruptof Oct 24th, 2006 4:50 PM

class errors
 
:

//SCRIPT NAME=evalcls.inc
<?php
class evalcls
{
        $var=1;
}
?>


INVOKED BY THIS SCRIPT

:

//SCRIPT NAME=hello.php
<?php
      include ("evalcls.inc");
      $oj= new evalcls();
?>


when i run this script i get this error;

:

Parse error: parse error, unexpected T_VARIABLE, expecting T_FUNCTION in C:\wamp\www\evalcls.inc on line 4

I am using the WAMP 5 tool as my main tool and i am writing the scripts with the crimson editor.

any help would be greately appreciated.

Jimbo Oct 24th, 2006 4:58 PM

:

<?php
  class foo
  {
    $something = 1;
    function __construct()
    {}
  }
  $f = new foo();
?>

yielded the same as yours, but this worked:
:

<?php
  class foo
  {
    public $something = 1;
    function __construct()
    {

    }
  }
  $f = new foo();
?>

Maybe PHP now requires access modifiers for variables?
[edit:] confirmed:
Quote:

Originally Posted by http://us3.php.net/manual/en/language.oop5.visibility.php
Class members must be defined with public, private, or protected.


DaWei Oct 24th, 2006 5:07 PM

From the manual:
Quote:

Originally Posted by "PHP Manual
The visibility of a property or method can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere. Protected limits access to inherited and parent classes (and to the class that defines the item). Private limits visibility only to the class that defines the item.

Further:
Quote:

Note: The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its usage would generate an E_STRICT warning.

kruptof Oct 24th, 2006 5:16 PM

thanks a bunch dawei and jimbo, should of remebered that from my c++ days;

kruptof Oct 24th, 2006 5:26 PM

wait sorry hold the applause:

new problem:

now the script is complaining about the next line in the script with the same error message:

:

public $ret = 0;
if($ret==0) //its complaining about this line:
{

}


and its giving this error message;

:

Parse error: parse error, unexpected T_IF, expecting T_FUNCTION in C:\wamp\www\evalcls.inc on line 5

anymore replys would greately be appreciated

grimpirate Oct 24th, 2006 5:42 PM

I believe the only change you need to make to your code is as follows:
:

class evalcls {
        var $foo = 1;
}

Where foo can be whatever name you choose for the variable, but I do believe the var keyword needs to be included rather than $var as you were doing before. Or as Jimbo stated placing the access type of the variable (public/private). So an alternate way might be:
:

class evalcls {
        public $foo;
        function __construct() {
                $this->foo = 1;
        }
}

Which will give your variable the necessary value upon its construction. I assumed you'd declared it as public 'cause you want to modify it somehow.

grimpirate Oct 24th, 2006 5:45 PM

Is that code located in your class or where exactly? 'Cause it wouldn't make sense to declare it as public if it's not in a class, which is what might be producing the error.

kruptof Oct 24th, 2006 5:54 PM

sorry i should it made clear. that code is part of the class.


thanks.

kruptof Oct 24th, 2006 5:57 PM

i tried that and i am still getting the same error grimpirate.

DaWei Oct 24th, 2006 5:58 PM

if ($this->ret == 0)....


All times are GMT -5. The time now is 1:11 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC