By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,215 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,215 IT Pros & Developers. It's quick & easy.

Can you share variables across objects created from the same class?

P: n/a
Hello all,
Can someone please tell me if there is a way to declare variables
within a class that can be shared among each new object created from
this class?
For instance I have the following:

class DB {
var $db;
var $host;
var $un;
var $pw;
var $connected;
var $connections;

function DB() {
$host="myserver.com";
$un="username";
$pw="password";
$this->db=null;
}

function Connect($database) {
if(!$connected) {
/* do your connection stuff….. */
$connected=true;
$connections=1;
}
$this->db=$database;
$connections++;
}
}

so that the variable $db which holds the name of the currently
selected database will change with each instance of an object being
created from this class, but the variable $connected and $connections
will be shared between each instance of an object.

Thank you in advance for your answers.
Anthony
Jul 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Anthony Davis wrote on Tuesday 05 August 2003 09:56:
so that the variable $db which holds the name of the currently
selected database will change with each instance of an object being
created from this class, but the variable $connected and $connections
will be shared between each instance of an object.


I may have misunderstood what you are saying, but I think a cleaner way to
accomplish this would be to

- create a ConnectionPool class which holds $connected, $connections and
other related properties, and also has an array of DBConnection objects;
- create a DBConnection class (similar to your DB class) and add an instance
to the array in ConnectionPool for every new connection.

You may even have a property within the DBConnection class that references
the ConnectionPool object(s) that it belongs to for easy access from
DBConnection.

I don't know what your final goal is, but I thought that's where you are
headed.

--
Business Web Solutions
ActiveLink, LLC
www.active-link.com/intranet/
Jul 16 '05 #2

P: n/a
Nikolai Chuvakhin wrote:
ad****@addpower.com (Anthony Davis) wrote in message
news:<e5**************************@posting.google. com>...

Can someone please tell me if there is a way to declare variables
within a class that can be shared among each new object created from
this class?


Not really, unless you want to declare them as global...

Cheers,
NC


You have to wait till PHP 5 for static/class variables
--
Matt Mitchell - AskMeNoQuestions
Dynamic Website Development and Marketing
Jul 16 '05 #3

P: n/a
matty <ma*******@askmenoquestions.co.uk> wrote in message news:<K3*********************@stones.force9.net>.. .
Nikolai Chuvakhin wrote:
ad****@addpower.com (Anthony Davis) wrote in message
news:<e5**************************@posting.google. com>...

Can someone please tell me if there is a way to declare variables
within a class that can be shared among each new object created from
this class?


Not really, unless you want to declare them as global...

Cheers,
NC


You have to wait till PHP 5 for static/class variables


Does anyone know when PHP 5 is scheduled for release?
thanks
Jul 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.