The issue here is that you define those functions in the scope of one method, whilst trying to access them inside the scope of another method. Those are two separate things.
To accomplish something like that you have to either declare the variable in the global scope (considered bad practice), or store it inside the objects properties.
Consider this example:
<?php
$varInGlobal = "I am defined globally";
class myClass {
private $varInClass = "I am defined in the class";
private $varFromFile;
function __construct() {
require 'includeVar.php';
$this->varFromFile = $varInFile;
echo "## in constructor: \n";
var_dump($varInFile);
}
function showMyVars() {
echo "** in class: \n";
var_dump($varInClass);
var_dump($this->varInClass);
echo "** in global: \n";
var_dump($varInGlobal);
var_dump($GLOBALS['varInGlobal']);
echo "** in file: \n";
var_dump($varInFile);
var_dump($GLOBALS['varInFile']);
echo "** in property: \n";
var_dump($this->varFromFile);
}
}
$obj = new myClass;
$obj->showMyVars();
The included file includeVar.php
:
<?php
$varInFile = "I am defined externally";
The output is:
## in constructor:
string(23) "I am defined externally"
** in class:
NULL
string(25) "I am defined in the class"
** in global:
NULL
string(21) "I am defined globally"
** in file:
NULL
NULL
** in property:
string(23) "I am defined externally"
This may not be convenient or what you expected, but this is how object orientation works. Which is why it is not a common practice to "outsource" variable declarations to separate files.
Instead you can "inject" such variables if you really have to keep them separate by handing them over to the constructor of your class. Or to a factory associated with that class.