Passing PHP MAGIC CONSTANTS or $GLOBALS

165 Views Asked by At

How do I possibly use one of these vars? I've tried many versions and keep getting errors

public $currentDir = $_SERVER['DOCUMENT_ROOT'];
class myClass {
var $users_xml_file = $currentDir."data/my.xml";

var $currentDir = __DIR__;
class myClass {
var $users_xml_file = $currentDir."data/my.xml";

$currentDir = dirname(__FILE__);
class myClass {
var $users_xml_file = $currentDir."data/my.xml";

class myClass {
$currentDir = dirname(__FILE__);
var $users_xml_file = $currentDir."data/my.xml";
1

There are 1 best solutions below

0
sectus On

Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

Properties

For fast solution you could set your variable via constructor.

$currentDir = $_SERVER['DOCUMENT_ROOT'];
class myClass {
var $users_xml_file = null;

public function __constructor(){
global $currentDir;
$this->users_xml_file = $currentDir."data/my.xml";;
}