I have a class Page that creates an instance of DB, which is named $db.
In the __construct() of Page, I create the new $db object and I pull a bunch of config data from a file.
Now the DB class has a method _connectToDB() which (attempts) to connect to the database.
Is there a way in the DB class to call the parent class’s config array? I don’t want to make global variables if I don’t have to and I don’t want to grab the config data twice.
Pseudo code might look something like this…
$dbUsername = get_calling_class_vars(configArray['dbUserName']);
I find that it’s often easier to initialise all the ‘important’ objects close to whatever variables they need to know. You could try it this way:
Doing it this way means you can also do type-checking on the database object if you want to: