Robert Saylor
asked on
php construct
I am passing mysql connection to my class and has been working well. However, I am now starting to use multiple databases.
So my question I know you can only have 1 construct. How can I write the above to be able to call $linkID and $linkID2 ?
$linkID = new mysqli(HOST, USER, PASSWORD, DATABASE);
$linkID2 = new mysqli(GOOGLE_HOST, GOOGLE_USER, GOOGLE_PW, 'database');
$myclass = new loader($linkID,$linkID2);
class loader {
public $linkID;
function __construct($linkID){ $this->linkID = $linkID; }
}
So my question I know you can only have 1 construct. How can I write the above to be able to call $linkID and $linkID2 ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
this lead me to thinking GLOBALS
You should always avoid to use global variables. A more correct and secure design pattern is the one I suggested above: that way, DB connections are used internally by the class. Even better should create the connections within the class itself defining them as private in order to better encapsulate them.
Wanting being pities, we could imagine a specific class wich create a new connections based on provided data. This class should be used by any class need to access the database.
Anyway, I discourage you from using GLOBALS.
Wanting being pities, we could imagine a specific class wich create a new connections based on provided data. This class should be used by any class need to access the database.
Anyway, I discourage you from using GLOBALS.
Why not this
$linkID = new mysqli(HOST, USER, PASSWORD, DATABASE);
$linkID2 = new mysqli(GOOGLE_HOST, GOOGLE_USER, GOOGLE_PW, 'database');
$myclass = new loader($linkID,$linkID2);
class loader {
public $linkID1;
public $linkID2;
function __construct($linkID1, $linkID2)
{
$this->linkID1 = $linkID1;
$this->linkID2 = $linkID2;
}
}
ASKER
I called the following outside of the class:
Open in new window
I am now able to call $GLOBAL['linkID6'] inside my class.