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

  • SEARCH
  • Home
  • 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 6988623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:06:20+00:00 2026-05-27T19:06:20+00:00

I have a superclass which instantiates my Sql class so all derived classes will

  • 0

I have a superclass which instantiates my Sql class so all derived classes will be able to perform queries without instantiating their own Sql instances once they are constructed.
This works good but I have a question I’m confusing myself about. One of the derived classes instantiates another derived class in its constructor so now, I believe I have 2 instances of the Sql class open because each instantiates the parent. I cannot override the parent class in any of the derived classes because they all need access to a a database instance to perform their own queries. Is there a way to prevent the derived class from calling the parent constructor when another derived class instantiates it?

I’m thinking passing a parameter to the class and only calling the parent constructor if meets the requirements as such.

function__construct($param) {
    if($param == false) {
        parent::__construct();
    }
 }

Do you think this approach makes sense, or do you forsee any bugs arising in the future?

  • 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-27T19:06:21+00:00Added an answer on May 27, 2026 at 7:06 pm

    I think what you’re trying to do is using the singleton pattern. To do that, you should just declare your constructor as a private method:

    private function __construct() {
    
    }
    

    and initialize it once by calling a static method for example:

    static function connect_to_mysql() {
        //procedure of connecting...
    }
    
    myClass::connect_to_mysql();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some classes which extends a superclass, and in the JSP I want
I have an AbstractEntity class as superclass for all my entites that defines an
I have several classes, that all derives from SuperClass. When the classes are created,
I have a class which I cant change (SuperClass) that has an interface for
I have a class which i realized will not always correctly instantiate and as
I have a class derived from base class which has a private field. How
I have some class inheritance SubClass < MidClass < SuperClass and want to perform
I have a list of classes that extend the same superclass, which has a
Say I have a class named Item. Which is a superclass of NewsItem and
I have set of classes which inherit from a single super class: Super |

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.