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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:46:03+00:00 2026-05-12T06:46:03+00:00

I have an includes.php page that I load at the start of every page

  • 0

I have an includes.php page that I load at the start of every page of my website.
As I develop the website, the number of classes that I am using is growing.
So I end up with something like this:

$db = new DB($config);
$login = new Login($db, $config);
$form = new Form($db, $config);

And the list goes on and on.
I have two questions about this practice:

First, Considering I might not be using a class at a certain page (I might not have a $form on every single page), how much does it really matter, performance-wise, to load this class every time any given page is loaded?

Second, you may have noticed that I am passing the class instance $db to all the other classes, as well as a variable $config. In the php code of every class, I do something like this:

public $db;
public $config;

public function __construct($db, $config, $smarty){
    $this->db = $db;
    $this->config = $config;
}

then in the class methods, I address the database and config files with ‘this’ as such:

public function myfunction(){
  $this->db;
  $this->config;
}

When should I use ‘extends’ rather than passing $db to the class, assuming every class uses the db? Does passing $db to every class hurt in any way in terms of performance?

Thanks!

  • 1 1 Answer
  • 1 View
  • 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-12T06:46:03+00:00Added an answer on May 12, 2026 at 6:46 am

    When should I use ‘extends’ rather
    than passing $db to the class,
    assuming every class uses the db?

    When it makes sense — and only when it does !

    You have at least two things to consider :

    • “class A extends B” kind of means “class A **is a** B”
      • more clearly, a Car is a MotorVehicule ; a MotorVehicule is a Vehicule ; a Bus is a MotorVehicule ; a Bicycle is a Vehicule
      • however, a Ball is not a Vehicule
      • In your case, a Form is definitly not a DataBase ! Nor is a Login
    • In PHP, a class can only extend one class
      • You can not have something being both a Vehicule and an Animal
      • But a Car is a MotorVehicule, which, itself, is a Vehicule 🙂

    In the case of a Database object (in your case, it’s more a connection to a DB), mosts of your classes will not themselves “be” a database connection. So, they shouldn’t extend that class.

    However, they are using a DB connection (a Form “has a” DB connection) ; so, they should have a property representing that DB connection. That’s what you are doing.

    Instead of passing $db to each constructor, you might use

    • either the Singleton design pattern
    • or the Registry design pattern
    • or some kind of global variable, but that’s almost the same… just being worse (not OOP and all that) !

    But passing the $db object is great for unit-testing, mock objects, and all that…

    I think it could be considered as being the Dependancy Injection design pattern, btw (not sure, but looks like it)


    About loading lots of classes, other people gave answers :

    • Use autoloading if you can
    • Use an opcode cache, like APC, if you can

    Both of those are great suggestions that you should take into consideration 😉


    One last thing :

    Does passing $db to every class hurt
    in any way in terms of performance?

    Maybe it does a little bit ; but, honnestly, except if you are google and have millions of users… who cares ?

    If you are doing a couple of DB queries, those will take LOTS of time, comparing to passing one more parameter to even a dozen methods !

    So, the small amount of time used passing paremeters can probably be neglected 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a live site that includes different php files depending on what page
I have a php page (horses.php) that includes: <?php require_once('send_email.php');?> What happens is I
I have an index.php that includes config.php . Now I'm using $.get() when I
I have a index.php page that is the main page. All pages are included
I have a PHP script (index.php) that includes an index.html file in a sub-directory
My problem: require_once '/includes/aws-sdk-1.5.2/sdk.class.php'; My environment: I have a pretty standard PHP site that
Within my HTML, I have a php script that includes a file. At that
I have an index.php, which at it's very start includes db.php - file which
I'm creating an events website using PHP. I have a database with an events
The setup: I have a standard .php file (index.php) that contains two includes, one

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.