views:

239

answers:

3

So I have a class.. In its constructor I include the code which connects me to my database via the mysqli extension:

class MyClass
{
    public function __construct()
    {
        include("dbconnect");
    }
}

dbconnect looks like this:

$host = "localhost";
$user = "user";
$pass = "123";
$database = "myDatabase";

$mysqli = new mysqli($host, $user, $pass, $database);
$mysqli->set_charset('utf8-bin');

Now to my problem: Since mysqli can be used OOP-Style, how do I get access to the variable in MyClass?

function doIt()
{    
    $query = "SELECT * FROM myTable";    
    $result = $mysqli->multi_query($query);
}

A call to this function results in

Notice: Undefined variable: mysqli in ... on line ... Fatal error: Call to a member function multi_query() on a non-object in ... on line ...

So it seems the scope of the variable is not right. Does anyone know how to fix this? It would be best if MyClass would not need an extra reference or something to mysqli, since I would like to keep it seperated.

+5  A: 

The $mysqli variable is only available inside the scope of the constructor. Change your constructor like so:

class MyClass
{
    public function __construct()
    {
        include("dbconnect");
        $this->mysqli = $mysqli;
    }
}

Now you can use $this->mysqli in other methods on that object.

gnud
alright, thats ok I can live with that :) Thank you very much!!
Thomas
+1  A: 

The variable has the same scope as any other variable inside a function: it is only valid inside the function. As soon as the function returns, it's gone. If you want to "persist" a variable for other function in the class, make it a Class member:

class MyClass {
    var $member = null;

    function foo() {
        $localVar = $this->member;
        $this->anotherMember = 'bar';  // $anotherMember is now available for other functions
    }
}

Reusing code via an include is not good precisely because it doesn't give you any control over how the variables will be used. I'd think about restructuring the thing, like making a function that establishes the DB connection, then returns the DB handle.

deceze
yeah, could do that.. maybe I will.. But since I'm using include only to manage my db-connection within one file, and for nothing else (and never will for nothing else ;) ), I think it's ok.. Yes, I could put the whole thing in a Connection-Class, but I'm lazy ^^
Thomas
@Thomas Laziness is a necessary attribute for a programmer, but it's also what'll make his programs a mess... ;)
deceze
You're absolutely right. Often enough I was annoyed by myself because of that. And regarding this, I will put the connection into a dedicated class, even if there's not really a good point (except for the mentioned above) doing this ;)
Thomas
A: 

What you're looking for is the "global" keyword. In general, though, I would avoid globals and rethink your design.

blockhead
no, I did not look for the word global.. I looked for something like the posts above....
Thomas