tags:

views:

64

answers:

3

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...
I think this is a stupid question and I'm too tired, but I have to finish. Where I'm doing wrong? Thank you

+1  A: 

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.

Peter Bailey
I've added the connect
Mihai Iorga
If you have other methods that run stuff like `mysql_query` make sure they use `$this->db_connect_id`
Peter Bailey
the mssql_query is made by a function in the SQL class, and it uses $this->db_connect_id
Mihai Iorga
+1  A: 

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() }
}
Jon Cram
thx for the tip but i've tried, same issue :|
Mihai Iorga
Are you using the `$this->database` reference when making the SQL calls?
Sonny
i've added sql_query also
Mihai Iorga
@Mihai Iorga: In which case you'll need to update your question with a more accurate picture of the code you are using. Your description of the problem is /exactly/ what is expected of static properties.
Jon Cram
that is an accurate picture of the code, it just simply looses the connection from first database and all is set by the second class
Mihai Iorga
+3  A: 

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).

petraszd
I didn't see that one coming :) good point and thank you
Mihai Iorga