title: php: efficiently running functions with one-time loaded classes multiple times in optional files
body: <p>after reading the responses I have rewritten my question.</p>
  
  <p>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.</p>
  
  <p>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)</p>
  
  <pre><code>$user = new userClass($db);
  $user-&gt;login($credentials);
  </code></pre>
  
  <p>all is fine, and I can even repeat the procedure several times after creating the user class</p>
  
  <pre><code>$user = new userClass($db);
  $user-&gt;login($credentials);
  ...
  $user-&gt;login($credentials2);
  </code></pre>
  
  <p>now imagine if these 2 parts are split in 2 different php files.</p>
  
  <pre><code>file1:
  $user = new userClass($db);
  $user-&gt;login($credentials);
  file2:
  $user-&gt;login($credentials2);
  
  include file1
  include file2
  </code></pre>
  
  <p>if they are run in order all is fine, but if they are not, or if file1 is not included at all...</p>
  
  <pre><code>file2:
  $user-&gt;login($credentials);
  file1:
  $user = new userClass($db);
  $user-&gt;login($credentials2);
  
  include file2
  include file1
  </code></pre>
  
  <p>then it won't work... they have to maintain order, so lets make a main php file that gets loaded no matter what.</p>
  
  <pre><code>main file:
  $user = new userClass($db);
  file1:
  $user-&gt;login($credentials);
  file2:
  $user-&gt;login($credentials2);
  </code></pre>
  
  <p>(included files with autoloading for example)
      include main
      include file1
      include file2
      or
      include main
      include file2
      include file1
      or
      include main
      include file2</p>
  
  <p>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?</p>
  
  <pre><code>main file:
  $user = new userClass($db);
  //nothing else gets loaded
  </code></pre>
  
  <p>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?</p>
  
  <p>not elegant at all.</p>
  
  <p>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):</p>
  
  <pre><code>file1:
  if(!is_object($user)){
      $user = new userClass($db);
  }
  $user-&gt;login($credentials);
  file2:
  if(!is_object($user)){
      $user = new userClass($db);
  }
  $user-&gt;login($credentials2);
  </code></pre>
  
  <p>sure, it works, but it's not elegant, now is it?</p>
  
  <p>let's try it with method overloading...</p>
  
  <pre><code>class loader {
     private $objects;
     private $db;
     function __construct($db){
        $this-&gt;objects = array();
        $this-&gt;db = $db;
     }
     function __get($name){
        if(!isset($this-&gt;objects[$name])){
           return $this-&gt;objects[$name] = new $name($this-&gt;db);
        }
     }
  }
  
  main file:
  $loader = new loader($db);
  file1:
  $loader-&gt;user-&gt;login($credentials);
  file3:
  $loader-&gt;user-&gt;login($credentials3);
  file2:
  $loader-&gt;user-&gt;login($credentials2);
  file4:
  $loader-&gt;user-&gt;login($credentials4);
  </code></pre>
  
  <p>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</p>
  
  <p>and a simple script like this:</p>
  
  <pre><code>$user = new userClass($this-&gt;db);
  $user-&gt;login($credentials);
  $form = new formClass($_POST);
  $form-&gt;process($info);
  </code></pre>
  
  <p>will require 2 separate loader classes or at least 2 methods in the loader class</p>
  
  <pre><code>   class loader {
         private $objects;
         private $db;
         function __construct($db){
            $this-&gt;objects = array();
            $this-&gt;db = $db;
         }
         function getuserclass($name){
            if(!isset($this-&gt;objects[$name])){
               return $this-&gt;objects[$name] = new $name($this-&gt;db);
            }
         }
         function getformclass($name){
            if(!isset($this-&gt;objects[$name])){
               return $this-&gt;objects[$name] = new $name($_POST);//just an unrealistic example.
            }
         }
      }
  
  main file:
  $loader = new loader($db);
  file1:
  $loader-&gt;getuserclass('user')-&gt;login($credentials);
  file3:
  $loader-&gt;getformclass('form')-&gt;process($info);
  </code></pre>
  
  <p>but this is not elegant either.</p>
  
  <p>How is this <strong>really</strong> supposed to be done?</p>