How do I make a variable that is created within a class function available outside of that class function?
For instance, the constructor of my main class creates an instance of the logging class. I want this $log variable to be available in other classes (using the global keyword) I would assume.
Is this possible?
You can use static.
class Main
{
static private $log;
public function __construct()
{
self::$log = new Log();
}
static public function getLog()
{
return self::$log;
}
}
Now you can get access to $log everywhere using
Main::getLog();
Using a global would defeat the purpose of OOP. OOP is meant to alleviate dependence and provide code containment for modular use.
You are simply approaching your application design wrong, instead, design ease of access to the logging instance. That is; instantiate your logging class and store the instance somewhere that's accessible (by design) by other classes that depend on it. A sort of "registry".