开发者

Using a class in PHP4 and i don't know how to set a method

in PHP 5 it would be

class Example {   
  privat开发者_运维技巧e $foo = "old data";
  public function __construct(){}
  public function setVar($data){
      $this->foo = $data;
  }
  public function output(){
    return $this->foo;
  }
}
$var = new Example();
$var->setVar("new data");
echo $var->output();

I never learned OO in PHP 4 and am having trouble finding where to do this. Working with a class that I have to extend a bit. Any searches on this shows me a ton of ways to do this in PHP5 but not 4

Thanks


To make this code PHP 4 compatible, you'd have to

  • remove the private public protected keywords

  • change private $foo = to var $foo =

  • change __construct() into Example() (the constructor is a method named after the class name)

but much more importantly, why do you need this? PHP 4's time is over. Except for historical purposes, there should be no need for new PHP 4 code any more. If you have a web host still running PHP 4, leave 'em.


See PHP Manual on Classes and Objects in PHP4 and Migrating from PHP 4 to PHP 5.0.x

class Example
{
  var $foo = "old data";

  function Example() {}

  function setVar($data){
      $this->foo = $data;
  }

  function output(){
    return $this->foo;
  }
}

$var = new Example();
$var->setVar("new data");
echo $var->output();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜