I need a little help in accessing my connection object from another class. I've defined this class:
class CurrentEnvironment {
public static $connection = null;
static public function connect() {
// we don't need to connect twice
if (self::$connection ) {
return self::$connection;
}
// data for making connection
$serverName = "(local)";
$connectionInfo = array( "Database"=>"E-TICKET");
// try to connect
self::$connection = sqlsrv_connect($serverName, $connectionInfo);
if(!self::$connection ){
return null;
} else
{
return self::$connection;
}
}
The problem is that every time I call the CurrentEnvironment::connect() a new connection is generated. But I think that I don't need to connect twice..
Is there a way I can use to connect once and get a reference to the connection object every time I need it?
PS: I'm new in PHP object oriented