简体   繁体   中英

How to use a PHP object created inside a function outside of that function?

I am learning PHP and trying to wrap my head around both PHP and OOP. I've created this function to allow me to select which database to connect to and then create an object. I want to then use that object outside of the function to run queries, but I'm not quite sure how to do this. Here's what I've got:

function connectToDB($database) {
    switch($database) {
        case 'DB1':
            $host = DB1_HOST;
            $user = DB1_USER;
            $pw = DB1_PW;
            $dbname = DB1_NAME;
            $port = DB1_PORT;
            break;
        case 'DB2':
            $host = DB2_HOST;
            $user = DB2_USER;
            $pw = DB2_PW;
            $dbname = DB2_NAME;
            $port = DB2_PORT;
            break;
    }

    $db = new MySQLi;
    $db->connect($host, $user, $pw, $dbname, $port);
    return $db;
}

So what I'm trying to do is tell the function to either connect to 'DB1' or 'DB2', create a MySQLi object and make a database connection for that database, and then give me back the object ($db) so that I can do other things with it outside of the function, but I can't figure out how to get $db to exist outside of this function.

You could do this:

$dbObj = connectToDB('DB1');
$dbObj->someFunction();

The function ConnectToDB() returns an instance of MySQLi. You have to assign the returned instance to a variable. This can be done by doing:

$my_object = connectToDB('DB1') //in case of DB1

Then you can simply call the methods defined on the object in the following way:

$my_object->method_to_call();

In other words, you let a specific instance of a certain class (MySQLi) do some action, method_to_call() in this case.

There are several methods you could use. The one you are using should work as long as you are assigning the returned object to a variable. Another method would be to create the object somewhere else in a higher level class and then use the object variable in the connection method (this code probably doesn't work, it's just to illustrate the concept).

// declare database connection variable outside of functions in class 
private $db;
...

function init() {
   ...
   $this->db = new MySQLi; // or call this inside the connection function
}

function connectToDB($database) {
   $this->db->connect($host, $user, $pw, $dbname, $port);
}

Another method would be to pass the object in by reference to the function:

$db = new MySQLi;
function connectToDB($database, &$db) {
   // the & in front of the param indicates that it is passing in the original object by reference instead of making a copy in the local scope.
   $db->connect($host, $user, $pw, $dbname, $port);
   // since the object is passed in by reference, we don't need to return it
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM