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 6745323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:11:27+00:00 2026-05-26T12:11:27+00:00

I have 4 classes, one Database helper and 3 defined like this: abstract class

  • 0

I have 4 classes, one Database helper and 3 defined like this:

abstract class PageElement
{
    protected $db;

    abstract public function reconstruct($from, $params = array());

    protected function construct_from_db($id, $k)
    {
        $this->db = new Database(0);

        $query = "SELECT * FROM $k WHERE id = %d";

        $results = $this->db->db_query(DB_ARRAY, $query, $id);

        return $results[0];
    }   
}


class Section extends PageElement
{
    function __construct($construct, $args = array())
    {
        $params = $this->construct_from_db($args, 'sections');
    }
    //other methods
}


class Toolbar extends PageElement
{
    function __construct($construct, $args = array())
    {
        $params = $this->construct_from_db($args, 'toolbars');
    }
    //other methods
}

Right now, each child has it’s own instance of Database object. How can I create the database object in the parent class and share it to each child?

NOTE:

  • I’ve read about the Singleton approach, but I can’t use it, since I
    have to connect to different databases.
  • Might be noteworthy that Section class creates an instance of Toolbar class.
  • Another problem is that, for some reason, I can’t close mysql connection. This warning appears when I run the code:

    mysql_close(): 7 is not a valid MySQL-Link resource in ****\classes\database.class on line 127.

  • 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-26T12:11:27+00:00Added an answer on May 26, 2026 at 12:11 pm

    Ideally you should create the database object outside these classes and then inject it via constructor or setter function.

    abstract class PageElement
    {
        protected $db;
    
        function __construct($db)
        {
             $this->db = $db;
        }   
        //...rest of the methods
    }
    
    class Toolbar extends PageElement
    {
        function __construct($construct, $db, $args = array())
        {
            parent::__construct($db);
            $params = $this->construct_from_db($args, 'toolbars');
        }
        //other methods
    }
    

    Then you can create your objects:

    $db = new Database(0);
    $toolbar = new Toolbar($construct,$db,$args);
    $section = new Section($construct,$db,$args);
    

    This way all the objects will share same database object.

    P.S.: Instead of creating database object using new here, you can get it from a parameterized factory.

    $db = Factory::getDBO($param);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one abstract class and many child classes. In child classes are from
In the projects I worked on I have classes that query/update database, like this
I have classes that are structured like the following: public class Forecast { [Key]
I have 3 classes (simplified for this question): public class Page { public Guid
I have a Userdetails class like the one below public class UserDetails { public
I have two classes. One is Order: public class Order { public virtual int
I have two classes, and want to include a static instance of one class
I have several classes that are basically interfaces to database rows. Since the class
I'm developing an application which will have these classes: class Shortcut { public string
I have two entity classes: public class Invoice { public int ID { get;

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.