Well in Java and Javascript there are system built-in objects accessible in any scope of the script. Some examples are System, Math in Java and Window, Document in Javascript. The good thing about these objects is that they can be accessed from both main script and functions/classes methods.
In PHP, however, I will have to declare such variables(database, page, template for instance) to be global in every function/method that uses it. The other approach is to inject dependency, but these are technically 'global' objects that are used in almost 50% of the functions/methods. The script will have to look like this below, quite tedious if you ask me:
$system = new System;
$database = new Database($dsn, $dbuser, $dbpass);
$page = new Page($system->getpage());
$template = new Template($system->gettemplate());
class Someclass{
public function __construct($arg1, $arg2...){
global $database, $page, $template;
// constructor code inside
}
public function somemethod($arg1, $arg2...){
global $database, $page;
// code inside
}
public function anothermethod($arg1, $arg2...){
global $database, $templage;
// code inside
}
// And more...
}
Sure I can define a database, page and template handler method for this class so that I only declare global variables once. Nonetheless, it is still tedious if I have like tens of classes like this. Note the idea of registering $GLOBALS is not acceptable since its insecure and poor programming practice...
So the question is, are there easier and more convenient ways to define system objects that can be used in any scope of the script: main script, function files, class files or whatever. Please lemme know if you can help, and Id appreciate every single line of comment. Thanks.