Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3974770
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:36:25+00:00 2026-05-20T04:36:25+00:00

I am extending a class from mysqli, but I am not sure if I

  • 0

I am extending a class from mysqli, but I am not sure if I am doing correctly when I want to close the db connection:

class database extends mysqli
{
    public function __construct($hostname = null,$username= null,$password = null,$database = null,$port = null, $socket = null)
    {
        $hostname = $hostname !== null ? $hostname : ini_get("mysqli.default_host");
        $username = $username !== null ? $username : ini_get("mysqli.default_user");
        $password = $password !== null ? $password : ini_get("mysqli.default_pw");
        $database = $database !== null ? $database : "";
        $port     = $port     !== null ? $port     : ini_get("mysqli.default_port");
        $socket   = $socket   !== null ? $socket   : ini_get("mysqli.default_socket");

        parent::__construct($hostname,$username,$password,$database,$port,$socket);


        # Check for connection error

        if (mysqli_connect_errno()) 
        {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }
    }


    #closes the database connection when object is destroyed.
    public function __destruct()
    {
        return parent::close();
        //echo "Destructor Called";
    }
}

do I have put return in front of parent::close();?

return parent::close();

or this will do?

public function __destruct()
        {
            parent::close();

        }

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:36:26+00:00Added an answer on May 20, 2026 at 4:36 am

    The idea with a destructor is that it is automatically called when the object goes out of scope. As such, there will be nothing to assign a return value to.

    The best practice is for destructors (and constructors) to not return anything (void).

    Program code should never call the destructor directly.

    Edit: So to directly answer your question, use your second example

    public function __destruct()
    {
        parent::close();
    }
    

    Regarding exceptions, try something like this in your constructor

    if (mysqli_connect_errno()) {
        throw new Exception('Connect failed: ' . mysqli_connect_error());
    }
    

    To catch the exception, simply use a try / catch block, eg

    try {
        $database = new database();
    } catch (Exception $ex) {
        echo $ex->getMessage(); // or whatever
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using my own class for database queries, extending mysqli: class iDatabase extends mysqli
I have a java class that extends from JPanel. The basic purpose of extending
I'd like to override a class method, not creating subclass/extending from a class. An
I have class SuperYo extending class Persona, like this: public class SuperYo extends Persona
I have a class from another library that is closed-source, but I want to
I have class called Database. public class Database { public Connection connect = null;
I have create a Java class extending LinearLayout as shown below public class CustomLinear
I have create a Java class extending LinearLayout as shown below public class News
I have a class generated from a WSDL that has a bunch of public
Why is implementing Runnable a better option than extending from Thread class?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.