Class variables and scope
| From: | Michael Davey | Date: | Fri, 31 May 2002 15:11:42 +0000 |
| Subject: | Class variables and scope | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-100102@lists.php.net to get a copy of this message | ||
Can anyone here shed some light on this issue?
I have a class which is responsible for it's own database access and (some
of it) is initalised as below:
<?
class myClass
{
var $conn;
var $sth;
// etc...
function myClass ($db)
{
$host = "localhost";
$user = "user_name";
$password = "password";
$this->conn = mysql_connect ($host, $user, $pwd)
or die ("Unable to connect to database");
$sel = mysql_select_db ($db, $this->conn);
if (!$sel) die ("Unable to select database");
return true;
}
// rest of the class
}
?>
Now, say for example the class is used as per the script below:
<?
require ("myClass.class");
$c1 = new myClass ("first_database");
$c2 = new myClass ("second_database");
// error here
$c1->doSomething();
?>
When I try to work with the first instance of my class, I get an error which
I finally tracked down to it trying to run it's SQL queries against the
database defined in $c2. Now, I was under the impression that class
variables were private to each instance, however, this seems to be acting
like a static var.
Help?!?
Mikey