Re: Re: Global variables in a class? Nested classes VS inheritance...

From: Date: Thu, 18 Mar 2004 17:20:33 +0000
Subject: Re: Re: Global variables in a class? Nested classes VS inheritance...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-180864@lists.php.net to get a copy of this message
Chris W. Parker wrote:
I make a database connection outside of my classes... lets call it $myDBConnection. I need to use this connection in a class nested in a class... Was wondering the most efficient way of doing this? I don't want to create a new db connection in the class. I want to use the existing one...
this is really a non-issue if you set up your classes correctly. your base class should be the one that handles all the database interaction. this class should have a member that stores the connection. your extended class should be the one that is more specific to the task at hand, i.e. managing a user. the extended class can access all the members of the parent class the same as if it were it's own. <?php class DB { var $connection; function set_conn() {
    $this->connection = "hello";
} } class User extends DB { function get_connection() {
    return $this->connection;
} } ?>
Any thoughts? Would it make things easyer if the base class inherited all nested classes instead of nesting them? Any help would be appreciated.
hopefully i've sufficiently answered your question. chris.
You may want to take a look at static variables. http://php.net/variables.scope Look for "static". Static vars may also be in classes. -- paperCrane <Justin Patrin>

« previous php.general (#180864) next »