0

I am trying to pass an object as a parameter for constructor in a class such as:

class godtoolkit{
    protected $var;
    public $explanation;

    public function __construct($mysqli){
        $this -> var = $mysqli;
    }

    public function armageddon(){
        $this -> var -> close();
        $this -> explanation = "The World Ended!";
        return true;
    }
}

$goddemands = new godtoolkit($mysqli); // $mysqli is a normal mysqli connection
if($goddemands -> armageddon()){ // Is this closing my database?
    echo($goddemands -> explanation);
}

The question is obvious in the code example, keep in mind that this is an example snippet but enquires exactly what i need.

4
  • 1
    What is exactly the question? Why calling close is closing your connection? Commented Mar 18, 2015 at 19:31
  • @RaelGugelminCunha Well the question is, is that $this -> var -> close(); actually closing my mysqli connection? Commented Mar 18, 2015 at 19:33
  • Because the $var object attribute has your $mysqli object, passed in constructor, and you're calling close() on it, and $mysqli->close() closes your connection. Commented Mar 18, 2015 at 19:38
  • @RaelGugelminCunha I was asking if it is closing it, not why, but thank you for the explanation, it's pretty descriptive. I was asking this because using an object inside an object is a little harder to think about logics. Commented Mar 18, 2015 at 19:41

2 Answers 2

1

Short: Yes, this will close the MySQLi connection.

Why: It is important to note that, in PHP, Objects are always passed by reference. That is to say, the variable is actually a link to memory space that represents that object, and wherever the value of that variable goes, it will always point to the same space in memory, and therefore, the same object.

Example:

The following illustrates the difference between passing by copy, pass by reference, and how objects work:

function copy_increment($int) {
    $int++;
}

function reference_increment(&$int) {
    $int++;
}

function object_increment($object) {
    $object->int++;
}

function maybe_destroy($object) {
    unset($object);
}

$object = new StdClass();
$object->int = 1;
$int_val = 1;

var_dump($int_val); // 1

copy_increment($int_val);  // Not passed by reference

var_dump($int_val); // 1

reference_increment($int_val); // Passed by reference

var_dump($int_val); // 2

var_dump($object); // int = 1

object_increment($object); // Always passed by reference

var_dump($object); // int = 2

// But here you can see that the parameters are
// still copies, but copies of the pointer and 
// un-setting only effects the current scope.
maybe_destroy($object); 

var_dump($object); // int = 2

You can see this in action here. What does this mean in your case? It means that, yes, when you pass the MySQLi connection and store it, and then close it, it will invariably close all copies of that object.

There is a catch here in that you can clone objects, but MySQLi is what they call an "un-clone-able object," and it will kill your app if you try... :-(

TO NOTE: The MySQLi connection does not "need" to be explicitly closed, and the connection will terminate when the object is destroyed (at the end of the application or during Garbage Collection), so you should never have to explicitly call close on it.

Another option would be to have a factory class or function that builds the connection on demand. Pass this object to the constructor, and store the unique connection on this object. I can not think immediately off the top of my head why this would be useful, but it would be fairly easy to set up, and would allow you to close connections all day long. ;-)

Really though, you don't need to close it manually, particularly if you are just starting out.

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

2 Comments

I am acually building an error handler function inside that class and what i want to achieve is to close the database when an error have occurred (avoiding overflow). That is why i need to manualy close the mysqli connection, and i didn't know if it's possible to have an object inside an object and how to access the data properly with 2x '->'.
I would suggest using Exceptions instead of closing the connection. You would wrap most (if not all) of your application execution in a try catch block, and on an error, throw an exception. This will serve to allow you to log the exception, and prevent any further application execution at the same time. The issue with closing the DB connection internally is that you may have code somewhere else that attempts to exec on the DB, and that will just cause more problems...
0

Yes, calling armageddon() will close mysqli connection passed in constructor of your class.

3 Comments

Also, it isn't currently clear for me how objects work with this object in object thing, can you tell me, is it closing the connection inside the class or it 'globally' closing it.
It works just like a normal variable. The connection gets closed globaly from whatever scope you calling mysqli->close()
I don't understand why stack overflow recommends not to be polite on comments, i will break some rules here and thank you anyway.

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.