Avoiding Multiple Class Instantiations of Single Class PHP?
<p>I've got a class I wrote to work with the front end (web browser side) of a shopping cart.  </p>

<p>It's fairly simple in that I send the class a product ID that I bury in the URL and then query a database populating the classes variables for use in retrieving the data through some public methods. </p>

<p>To interface with my actual physical web page I have a file I call viewFunctions.php.  Wherein I instantiate my class called ItemViewPackage():</p>

<pre><code>&lt;?php
require_once(dirname(__FILE__) . '/ItemViewPackage.php');
$viewObject = new ItemViewPackage($_GET['page']);
</code></pre>

<p>So, I have shoppingcartpage.php (the physical url) that requires the file viewFunctions.php that loads my class ItemViewPackage().</p>

<p>The output page shoppingcartpage.php calls functions like get_item_info('title') or get_item_info('price') which in the viewFunctions.php file is made like so:</p>

<pre><code>function get_info($type){
    echo $viewObject-&gt;get_info($type);
}
</code></pre>

<p>Now, right off the bat, this isn't working because, I assume, $viewObject is not global. So I make $viewObject global like so:</p>

<pre><code>function get_info($type){
    global $viewObject;
    echo $viewObject-&gt;get_info($type);
} 
</code></pre>

<p>But, this doesn't work either, I still get an error for "<code>Call to a member function get_info() on a non-object</code>"</p>

<p>Now, the only thing that works is:</p>

<pre><code>function get_info($type){
    $viewObject = new ItemViewPackage($_GET['page']);
    echo $viewObject-&gt;get_info($type);
}
</code></pre>

<p>But, I don't want to re-instantiate my object every time I make a call to this function (which is several times for several bits of information).  I'd rather instantiate once at the top of my viewFunctions.php doc and use that object every time I call this function.</p>

<p>Am I going about this completely wrong?</p>

<p>Thanks in advance.
DIAGRAM (hopefully it helps visualize)</p>

<p><img src="http://i.stack.imgur.com/4gQzv.jpg" alt="Diagram"></p>

<p><img src="http://i.stack.imgur.com/bfnP2.jpg" alt="enter image description here"></p>
