PHP5 Classes - Can you get a calling class's variables?

Okay, so I'm new to OO and still kinda new to PHP5.

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']);

Thank you

Answers


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:

/* Code to get config variables here */
$DB = new DB($config);
/* You might want to delete the database password from $config here */
$Page = new Page($config, $DB);

Doing it this way means you can also do type-checking on the database object if you want to:

class Page {
    function __construct(array $config, DBClass $db) { }
}

Need Your Help

PHP Get Subdomain But Not Actual Domain

php subdomain

I'm currently using the following to get the subdomain of my site

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.