开发者

PHP 4 - variables inside class

开发者 https://www.devze.com 2023-02-06 04:07 出处:网络
I have a class like class blah extends blahblah{ private $variable = \'5\'; function somefunction(){ echo $variable;

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 al开发者_StackOverflow中文版l class functions?


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;
  }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消