1

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.

2
  • 2
    just a warning: If your file is just named 'dbconnect' and nothing more, and it is placed inside the webroot, the webserver might decide to share your database password with the whole world. It's good practice to give configuration data a .php file type, so that if it's run by a web server, nothing happens. Commented Feb 24, 2010 at 2:26
  • thx for the tip :) Actually, it is not placed in the webroot, and protected by .htaccess, too. But I'm thinking of using a connection class, anyway. Commented Feb 24, 2010 at 2:53

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

alright, thats ok I can live with that :) Thank you very much!!
2

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.

3 Comments

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 Laziness is a necessary attribute for a programmer, but it's also what'll make his programs a mess... ;)
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 ;)
0

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.