tags:

views:

395

answers:

5

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

+2  A: 

Why not pass the config parameters to the connectToDb function or pass the config data to the constructor of the DB class.

And to directory answer the question: you don't know anything about the outside calling context in your current context.

grepsedawk
I could do that I just thought there might be an easier way....
alex
+1  A: 

See debug_backtrace() to get information about calling classes or objects.

Then see Reflection to get more information on the properties of a given class or object.

edit: But for what it's worth, I'd also recommend passing the specific parameters you need. Referencing the caller's data probably constitutes Content Coupling.

Bill Karwin
I've decided to go with the solution above I marked as accepted. I appreciate all your help so far Bill :)
alex
A: 

You can pass the child a reference to the parent. For example:

class Parent {
  function __construct() {
    $this->myChild = new Child($this);
  }
  public function doSomething() {}
}

class Child {
  function __construct(Parent $parent) {
    $parent->doSomething()
  }
}

You may find this odd, since you're still in the parent's constructor, and thus your parent object isn't fully constructed yet. But you can still use $this as a reference to yourself, even in the constructor. You just need to be careful that your child doesn't refer to things in the parent that haven't been initialized yet.

JW
+5  A: 

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) { }
}
Ant P.
This is pretty much what I suggested without any code :)
grepsedawk
This looks like what I should implement, and thanks for the tip for unset()'ing the db config vars, good thinking!
alex
A: 

In general there is no easy way to do this. but as a general design issue i can see why your avoid global variables, but in the case of application wide configuration it is perfectly reasonable to have global access to the properties.

you can either make your config data global or just pass the needed properties to the db object initialization.

luke