Jump to content

Referencing A Parent Class' Other Children


Millar

Recommended Posts

Hello,

 

I want class db (which extends class base) to be able to access class conf (which extends class base).

 

When I print out the information of the base (parent class) class, I can access variables/functions defined in the base, db and conf classes.

 

When I print the information of the db class (child of base) I can access variables/functions of only the base and db classes. So why can I not reference variables from my config class from within the sibling class?

 

Thanks.

 

Here is the print_r output:

conf Object
(
    [db_host] => localhost
    [db_user] => *
    [db_pass] => *
    [db_database] => *
    [db_prefix] => *
    [db_type] => *
    [version] => 1.0
)

db Object
(
    [connection] => 
    [version] => 1.0
)

base Object
(
    [version] => 1.0
    [conf] => conf Object
        (
            [db_host] => *
            [db_user] => *
            [db_pass] => *
            [db_database] => *
            [db_prefix] => *
            [db_type] => *
            [version] => 1.0
        )

    [db] => db Object
        (
            [connection] =>
            [version] => 1.0
        )

)

Why does db Object not look like this?:

db Object
(
    [connection] => 
    [version] => 1.0
    [conf] => conf Object
        (
            [db_host] => *
            [db_user] => *
            [db_pass] => *
            [db_database] => *
            [db_prefix] => *
            [db_type] => *
            [version] => 1.0
        )
)

Link to comment
Share on other sites

  • 2 weeks later...

I think I'm having a similar problem. I have a class, artist_control, which has an instance of another class, database, as a variable (for connecting to database, executing queries, etc). Also in my artist_control class, in one of the functions I have an instance of an artist class. My artist class needs to access my database class in order to run some SQL queries.

 

How can I do this? I tried using global $db; (the variable was set in artist_control) but that didn't work. Is it a good idea to pass in the $db variable (perhaps by reference)?

Link to comment
Share on other sites

Why can't your artist class create its own instance of the db?  Use a singleton pattern if you are afraid of multiple connections, but why should one class have to share with another?  What if your artist_control class changes completely to a different database type?

 

My thought would be to create layers, and don't make them dependent on each other.

Link to comment
Share on other sites

If you think about it from a semantic point of view, is "artist_control" really a descendent of "database" ? It USES a database, but as far as I can make out it doesn't DESCEND (i.e isn't a child) of database.

 

Why not have your artist_control simply use a database connection.

e.g.

class artist_control {
  private $dbconn;

  public function __construct(){
    $this->dbconn = new Database();
  }

  public function getArtists(){
    $artists = $this->dbconn->query("SELECT * FROM arists");
    return $artists;
  }
}

 

Even better (as brandon mentioned) have the Database class be a singleton, meaning you can use $this->dbconn = Database::getInstance(); and share this between any other classes that require db connectivity. This saves database connections (although php will merge them for you) and memory for allocation of new classes.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.