Exn
Exn

Reputation: 809

How do I include this variable inside this class?

my code looks like something like this:

require_once('class.php')


$test = "test"

class Issue extends class {

  var $value = $test;

  //more stuff included in this class

}

it works when var $value = "test"; but I really need it to take value that is outside the class, I tried using global but it doesn't work.

Upvotes: 0

Views: 34

Answers (1)

John Conde
John Conde

Reputation: 219834

You can make it a parameter of your class constructor:

require_once('class.php')

$test = "test";

class Issue extends class {

  public $value;

  public function __construct($test) {
    $this->value = $test;
  }
}

If Issue::value is public you can also just assign it's value directly:

$issue = new Issue();
$issue->value = $test;

You can also create helper methods (i.e. setters) or use magic methods to populate this value as well.

FYI, using var in classes in obsolete and has been for quite some time. i recommend getting up to speed with PHP current syntax.

Upvotes: 2

Related Questions