Hi,
I'm pretty new to PHP5/OOP, and I have this particular question about how to share information between two classes I have. This is my first project using OOP, so I'm not really sure where to go, and unfortunately my Wrox Pro PHP5 book isn't helping as much as I hoped it would.
One class is a SystemComponent class which, for now, just reads in my app's configuration file. Another class I'm working on is a database class (called DbConnector). The goal is to take the information read-in by SystemComponent, and supply DbConnector with the correct connection parameters.
I've come up with something that works, but I don't know if it's good/proper coding practice (which, ultimately is more important to me than just being able to hack something together). Am I doing this right?
Three files: index.php, SystemComponent.class.php, and dbConnector.class.php.
Below is all the relevant code.
index.php
require_once('includes/SystemComponent.class.php');
require_once('includes/dbConnector.class.php');
//reads config file upon construct
$system = new SystemComponent;
/**This is where the main point of my question is: is this the correct way to do this?
** or would I do better to just feed it something like $system->getSettings() ?**/
$DB = new DbConnecto($system);
SystemComponent.class.php
class SystemComponent {
private $_settings;
function __construct()
{
$this->setSettings();
}
function getSettings($var=NULL)
{
// if $var==NULL return ALL settings
//else return the value for the parameter $var.
}
function setSettings($var=NULL)
{
//basically, if it's NULL, set default settings, as supplied by config file
//else, change/add setting parameter.
}
dbConnector.class.php
/**I'm pretty sure I should put the "extends SystemComponent", correct? **/
class DbConnector extends SystemComponent {
private $_settings;
function __construct($system)
{
/**is this the proper way to pass info?**/
$this->_settings=$system->getSettings();
}
Thanks so much for your help! I really appreciate it! π
Take care, everyone.
~Cameron