I have two classes which both extend from SQL class
like this:
class SQL {
private $db_connect_id;
function connect($ad, $db, $us, $pa){
$this->db_connect_id = mssql_connect($ad, $us, $pa);
mssql_select_db ($db, $this->db_connect_id) or die('sql error');
}
function sql_query($query = ""){
unset($this->query_result);
if($query != ""){
$this开发者_运维百科->num_queries++;
$this->query_result = @mssql_query($query, $this->db_connect_id) or die('error query');
}
if($this->query_result){
unset($this->row[$this->query_result]);
unset($this->rowset[$this->query_result]);
return $this->query_result;
}
}
}
class WEB extends SQL {
function __construct(){ $this->connect(params) }
function __destruct(){ $this->disconnect() }
}
class AUTH extends SQL {
function __construct(){ $this->connect(params) }
function __destruct(){ $this->disconnect() }
}
the problem is that if I call both of them
$WEB = new WEB();
$AUTH = new AUTH();
the $WEB
won't work anymore. It loses its connection with the database and it changes the db_connect_id with the db_connect_id from AUTH...
http://php.net/manual/en/function.mssql-connect.php
There is fourth param new_link
that must be passed as true
(because default is false
).
It's impossible to tell from the limited code you've posted, but I think it's pretty clear that you have some singleton-like behavior going on i.e., all instances of SQL
and its subclasses are sharing references.
Maybe if you replaced bla bla
with the actual code we could narrow it down for you.
There is nothing wrong with the code as presented in your question.
The problem you are describing is symptomatic of using a static property of the SQL class, possibly to hold a database connection resource.
A static property is shared between all instances of a class, which is the behaviour you appear to be describing.
This type of design will work the way you want:
class SQL {
private $database;
function connect() {
$this->datavbase = ....;
};
}
class WEB extends SQL {
function __construct(){ $this->connect(params) }
function __destruct(){ $this->disconnect() }
}
class AUTH extends SQL{
function __construct(){ $this->connect(params) }
function __destruct(){ $this->disconnect() }
}
This type of design will not work the way you want:
class SQL {
private static $database;
function connect() {
$this->datavbase = ....;
};
}
class WEB extends SQL {
function __construct(){ $this->connect(params) }
function __destruct(){ $this->disconnect() }
}
class AUTH extends SQL{
function __construct(){ $this->connect(params) }
function __destruct(){ $this->disconnect() }
}
精彩评论