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

Refresh

April 2019

Views

479 time

1

in PHP 5 it would be

class Example {   
  private $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

2 answers

1

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.

0

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