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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:38:18+00:00 2026-05-12T20:38:18+00:00

I am a PHP beginner. I have developed a social networking website similar to

  • 0

I am a PHP beginner. I have developed a social networking website similar to Orkut in PHP.

The basic program flow of my website is redirect everything to index.php

The index.php determines the $_SERVER[‘REQUEST_URI’] and does what needs to be done and render the output.

I initiaze all the classes at the top of index.php I want to know if it is a good practice to do this or not.

The index.php starts like this :-

// all the configuration files
require_once ("siteconfig.php");

// include library files / classes
require_once "lib/auth.php";
require_once "lib/album.php";
require_once "lib/db.php";
require_once "lib/form.php";
require_once "lib/inbox.php";
require_once "lib/social.php";
require_once "lib/profile.php";
require_once "lib/user.php";
require_once "lib/settings.php";
require_once "lib/validate.php";
require_once "lib/logs.php";
require_once "lib/sms.php";



// initialize all the classes
$db = new db($dbuser, $dbpass, $dbname, $dbhost);
$validate = new validate();
$auth = new auth();
$user = new user();
$profile = new profile();
$social = new social();
$settings = new settings();
$usersearch = new usersearch();
$album = new album();
$logs = new logs();
$liveupdates = new liveupdates();
$sms = new sms();
  • 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-12T20:38:18+00:00Added an answer on May 12, 2026 at 8:38 pm

    Initializing all objects from the start is obviously a bad idea. Less obvious is that using “new” in your “main” code is not a brilliant thought either. Use a “loader” that provides you with objects (not classes) you need and avoid “new” outside of the loader.

     // WRONG
    
     class MainController 
     {
              function action_send_sms() {
                  require 'lib/sms.php'; // or use autoload
                  $sms = new MySmsClass();
                  $sms->send(....)
    
    
     // RIGHT
    
     class Loader
     {
            function sms_object() {
                  require 'lib/sms.php'; // or use autoload
                  return new MySmsClass();
            }
      ....
    
     class MainController 
     {
              function action_send_sms() {
                  $sms = $this->loader->sms_object();
                  $sms->send(....)
    

    // edit: why we would need this?

    Imagine you have a number methods in your Controller that use the Sms object. If you use “new” and an explicit class name (the “wrong” approach above), 1) you must provide initialization data (e.g. sms gateway address) for the object in each method, 2) you have to edit your Controller code in case class name or constructor signature changes and 3) your cannot temporary replace Sms object dependent on a condition (e.g. “DummySms” for testing on your local machine).

    With Loader class all issues are no problem, because you write all object initialization code exactly once.

    (More precisely this pattern is called ServiceLocator, see http://martinfowler.com/articles/injection.html for more details).

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

Sidebar

Related Questions

This is probably pretty basic, but I'm a beginner in PHP - I have
Over the last year I have moved from a beginner PHP developer to a
I just wrote a little application(4 page php), everything work fine, however, I have
i am a beginner in PHP.I have this query in my code. $qsq =
I am a dead beginner with PHP and have been reading through 'PHP for
First of all, I'm a beginner to PHP. And have posted a question here:
I'm a beginner to PHP and I have a quick question. Is it possible
I am just a beginner in php. I have tried to write code for
I have a form and php processing file, and everything seems to be working
I am a PHP beginner and saw on the forum this PHP expression: My

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.