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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:32:37+00:00 2026-06-06T06:32:37+00:00

In the pages so far, I have had used one connection class and one

  • 0

In the pages so far, I have had used one connection class and one class to hold the logic for the page. However, I now have a page where I need to use multiple classes based on different scenarios that may happen.

This is what I have been able to make work, but it seems like bad practice. I’ve searched around but can’t find a solution. So my question is, what is a better way of what I’m trying to do.

<?php
require 'class/connect.php';
$db = new Connect();
require 'class/normal-page.php';
require 'class/get-page.php';
?>

    <?php if (!isset($_GET['id'])) { ?>
        <?php $normalpage = new NormalPage($db); ?>
    <?php
        } else {
        $id = $_GET["id"];
        $getpage = new Getpage($id, $db);
    ?>

        <h1><?php echo $getpage->getTitle(); ?></h1>
        <p><?php echo $getpage->getDescription(); ?></p>
    <?php } ?>


class Normalpage {

    function __construct($db) {

        $query = 'SELECT something FROM table';
        $result = $db->query($query);
}
}

Hope that’s enough info to make sense. So in short, I include the connect class and use the variable in both other classes.

  • 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-06-06T06:32:40+00:00Added an answer on June 6, 2026 at 6:32 am

    This is a proper way of dealing with the database object. In fact, it is called the dependency injection design pattern, and is actually quite common. You might want to consider saving the constructor parameter $db to a member variable so other class methods may access the database via something like $this->db if you declare the member variable as private $db;.

    However, you might want to consider the factory design pattern, as it will help organize the classes for your pages. See this example from the above link:

    class Factory
    {
        public static function build($type)
        {
            $class = 'Format' . $type;
            if (!class_exists($class)) {
                throw new Exception('Missing format class.');
            }
            return new $class;
        }
    }
    
    class FormatString {}
    class FormatNumber {}
    
    try {
        $string = Factory::build('String');
    }
    catch (Exception $e) {
        echo $e->getMessage();
    }
    
    try {
        $number = Factory::build('Number');
    }
    catch (Exception $e) {
        echo $e->getMessage();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple javascript functions in my html page. One of them, by far
Okay - I have a dilemma. So far my script converts page titles into
I need to add css height to each page dynamically with jQuery. So far
I have had several occasions recently to access a specific class several times over
We have an ASP.NET application that users use to generate certain reports. So far
So far I've been setting up redirects for single pages on my ASP.NET site
I'm trying to allow admin to create pages on the root path. So far
The internet resources seem few and far between and the best MSDN page (as
I am trying to retrieve the full HTML of a page, so far I
I want to add a new form to the edit customer page, so far

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.