I have a static language class that pulls key-value pairs from a JSON file and places them in a property of this class. This happens before any other code/classes are run.
This class needs to be accessed by about half of all other classes and their methods.
In order to stay away from using new Language();
within every method/class constructor, I'm currently using something similar to skwee's answer in which he posts:
class Context {
public $application;
public $logger;
}
========
$context = new Context();
$context->application = new Application();
$context->logger = new Logger(...);
doFoo($context);
========
function doFoo(Context $context) {
$context->application->doStuff();
$context->logger->logThings();
}
However this has a severe annoyance when it comes to method calls within other classes, namely exceptions.
class A {
public function call(Language $lang) {
throw new Custom_Exception($error, $lang);
}
}
class Custom_Exception extends Exception {
public function __construct($error, Language $lang) {
parent::__construct($lang->vars->preError . $error);
}
}
Just to be able to use the static language property, the Language object needs to be passed twice, possibly three times in a few situations. I'm looking for a better/more efficient way.