I would like to know if there is a way to access a variable defined in an other file from a class in PHP.
Example :
file_01.php
<?php
$a = 42;
?>
file_02.php
<?php
require_once('file_01.php');
class mnyClass
{
private $myVar;
function __construct($var = $a)
{
$this->myVar = $var;
}
function getVar()
{
return $this->var;
}
function setVar($var)
{
$this->myVar = $var;
}
}
?>
Obviously, my class is more complicated. I have chosen this example for a better comprehension of what I try to do 😉
Thank you in advance.
You could access the variable via GLOBALS:
http://php.net/manual/en/language.variables.scope.php
EDIT: a little more detail-