RE: [PHP] Re: Global variables in a class? Nested classes VS inheritance...
| From: | Chris W. Parker | Date: | Thu, 18 Mar 2004 17:13:43 +0000 |
| Subject: | RE: [PHP] Re: Global variables in a class? Nested classes VS inheritance... | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-180860@lists.php.net to get a copy of this message | ||
>> 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.