3

I have a class like

class blah extends blahblah{

  private $variable = '5';

  function somefunction(){
    echo $variable;
  }
}

this works in php 5, but not in php 4. I get a error:

Parse error: parse error, unexpected
T_VARIABLE, expecting T_OLD_FUNCTION
or T_FUNCTION or T_VA....

I also tried with public and static. Same error.

How can I add a variable inside that class that I can access from all class functions?

moinudin
  • 134,091
  • 45
  • 190
  • 216
Alex
  • 66,732
  • 177
  • 439
  • 641
  • The *most important question* is, why do you still care about PHP4? – netcoder Jan 19 '11 at 23:52
  • 2
    Please upgrade to modern PHP version, PHP 4 is dead for 2.5 years now. Let it rest in peace. – StasM Jan 19 '11 at 23:53
  • I don't, but the application that has this function won't run only on my site – Alex Jan 19 '11 at 23:53
  • 2
    So? Then make a requirement, *needs PHP5*. This is only fair these days. PHP4 is indeed dead, most servers will run PHP5. And if not: Not your problem! – Felix Kling Jan 19 '11 at 23:55

2 Answers2

8

In PHP4, member variables are declared with var:

var $variable = '5';

But you still have to access it via $this->variable in your function (I think, I'm not so familiar with PHP4).

That said, if possible, upgrade! PHP4 and "OOP" is more pain than fun.

Update: Ha, found it, some documentation about Classes and Objects in PHP4.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
8

private is not a valid keyword in PHP 4 change it to var $variable = '5'; also the function is wrong it should be...

class blah extends blahblah{

  var $variable = '5';

  function somefunction(){
    echo $this->variable;
  }
}
DeveloperChris
  • 3,412
  • 2
  • 24
  • 39