Re: [PHP] Re: Global variables in a class? Nested classes VS inheritance...
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. 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: Global variables in a class? Nested classes VS inheritance...
>> 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. 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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Global variables in a class? Nested classes VS inheritance...
On 17 Mar 2004 Brent Westmoreland wrote: > I too have questions on how to handle this situation, any help would be > greatly appreciated. [Situation was how to use a single database connection inside a class nested within another class etc.] If you have a single DB connection open for the entire script why not use a global variable? There are good reasons for not overusing global variables but sometimes there are good reasons to use them too. -- Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Global variables in a class? Nested classes VS inheritance...
I too have questions on how to handle this situation, any help would be greatly appreciated. On 2003-07-30 11:30:54 -0400, [EMAIL PROTECTED] said: Hello, I'm lookin for some tips on the best way to do this... 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... If no one has a better Idea I'll pass the connection through the constructor of the first class, and then again into the constructor of the nested class... then I can query and use the database connection as $this->myDBConnection Just wanted to avoid that because there's about 5 db connections I'll have to pass in... Any thoughts? Would it make things easyer if the base class inherited all nested classes instead of nesting them? Any help would be appreciated. Thanks -Dennis -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php