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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:08:24+00:00 2026-05-15T23:08:24+00:00

after reading the responses I have rewritten my question. Let’s say I have a

  • 0

after reading the responses I have rewritten my question.

Let’s say I have a theoretical php application that uses objects that do different things.
For every page that gets loaded by the application, different scripts will be run.

now I made a simple php script that creates a simple object.
(this is all made up, I’m just trying to understand this before I code it)

$user = new userClass($db);
$user->login($credentials);

all is fine, and I can even repeat the procedure several times after creating the user class

$user = new userClass($db);
$user->login($credentials);
...
$user->login($credentials2);

now imagine if these 2 parts are split in 2 different php files.

file1:
$user = new userClass($db);
$user->login($credentials);
file2:
$user->login($credentials2);

include file1
include file2

if they are run in order all is fine, but if they are not, or if file1 is not included at all…

file2:
$user->login($credentials);
file1:
$user = new userClass($db);
$user->login($credentials2);

include file2
include file1

then it won’t work… they have to maintain order, so lets make a main php file that gets loaded no matter what.

main file:
$user = new userClass($db);
file1:
$user->login($credentials);
file2:
$user->login($credentials2);

(included files with autoloading for example)
include main
include file1
include file2
or
include main
include file2
include file1
or
include main
include file2

now file1 and 2 can be loaded in any order, and one does not depend on the other, but what if both file1 or file2 are unnecessary?

main file:
$user = new userClass($db);
//nothing else gets loaded

then the main file is also unnecessary, and will prove to be a resource hog, sure if its one class it’s no problem, but what if the main file loads hundreds of classes that never get used?

not elegant at all.

Then let’s try to do it another way, let’s scrap the main file completely and do it like this (below is the bare bone example of what I want to achieve):

file1:
if(!is_object($user)){
    $user = new userClass($db);
}
$user->login($credentials);
file2:
if(!is_object($user)){
    $user = new userClass($db);
}
$user->login($credentials2);

sure, it works, but it’s not elegant, now is it?

let’s try it with method overloading…

class loader {
   private $objects;
   private $db;
   function __construct($db){
      $this->objects = array();
      $this->db = $db;
   }
   function __get($name){
      if(!isset($this->objects[$name])){
         return $this->objects[$name] = new $name($this->db);
      }
   }
}

main file:
$loader = new loader($db);
file1:
$loader->user->login($credentials);
file3:
$loader->user->login($credentials3);
file2:
$loader->user->login($credentials2);
file4:
$loader->user->login($credentials4);

seems to work, until you realize that you can no longer give any of the objects that are created this way any other variable except $db.
This means that loader class is limited to use with user class only (for example)
because using loader class with any other class will require editing of the loader class

and a simple script like this:

$user = new userClass($this->db);
$user->login($credentials);
$form = new formClass($_POST);
$form->process($info);

will require 2 separate loader classes or at least 2 methods in the loader class

   class loader {
       private $objects;
       private $db;
       function __construct($db){
          $this->objects = array();
          $this->db = $db;
       }
       function getuserclass($name){
          if(!isset($this->objects[$name])){
             return $this->objects[$name] = new $name($this->db);
          }
       }
       function getformclass($name){
          if(!isset($this->objects[$name])){
             return $this->objects[$name] = new $name($_POST);//just an unrealistic example.
          }
       }
    }

main file:
$loader = new loader($db);
file1:
$loader->getuserclass('user')->login($credentials);
file3:
$loader->getformclass('form')->process($info);

but this is not elegant either.

How is this really supposed to be done?

  • 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-15T23:08:25+00:00Added an answer on May 15, 2026 at 11:08 pm

    This would probably work for you:

    class loader {
       private $objects;
    
       function __construct(){
          $this->objects = array();
       }
    
       function __call($name, $args){
          if(!isset($this->objects[$name])){
             return $this->objects[$name] = new $name($args);
          }
       }
    }
    $loader = new loader();
    
    $user = $loader->user($db);
    $form = $loader->form($form_args);
    

    If you want multiple instances you could perhaps include an additional first argument in each call to loader that is a name, and change the loader code to use the first arg as a key for the object, and pass the rest to the object’s constructor.

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

Sidebar

Related Questions

After reading a description about swapping pointer addresses on Stackoverflow, I have a question
I have a web application which uses URLs that look like this: http://library.example.com/Register.aspx?query=academic&key=586c70bb-5683-419c-aae9-e596af9ab66a (The
I have a WCF Web application that uses a WCF Class Library. My project
I have a question with fluent interfaces. We have some objects that are used
After reading over some of the other responses to this type of question I
After reading this article on thedailywtf.com, I'm not sure that I really got the
After reading the mkdir(2) man page for the Unix system call with that name,
After reading this: http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#how-to-use-it I came to the conclusion that it is not valid
I have code listed here: Threading and Sockets . The answer to that question
I started with this question: Multiple Javascript gadgets per page . After reading up

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.