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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:13:54+00:00 2026-05-21T09:13:54+00:00

I’ve got a class I wrote to work with the front end (web browser

  • 0

I’ve got a class I wrote to work with the front end (web browser side) of a shopping cart.

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.

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

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

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

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:

function get_info($type){
    echo $viewObject->get_info($type);
}

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

function get_info($type){
    global $viewObject;
    echo $viewObject->get_info($type);
} 

But, this doesn’t work either, I still get an error for “Call to a member function get_info() on a non-object“

Now, the only thing that works is:

function get_info($type){
    $viewObject = new ItemViewPackage($_GET['page']);
    echo $viewObject->get_info($type);
}

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.

Am I going about this completely wrong?

Thanks in advance.
DIAGRAM (hopefully it helps visualize)

Diagram

enter image description here

  • 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-21T09:13:55+00:00Added an answer on May 21, 2026 at 9:13 am

    What for do you need viewFunctions.php anyway? It’s only wrapping the ItemViewPackage. Remove that and use the ItemViewPackage directly, e.g.

    // shopping.php
    include_once 'ItemViewPackage.php';
    $viewObject = new ItemViewPackage($_GET['page']);
    <div><?php echo $viewObject->get_info('title'); ?></div>
    <div><?php echo $viewObject->get_info('price'); ?></div>
    

    Then you dont have to bother with globals or Singletons. If you dont want a second instance, dont instantiate a second one. It’s simple as that in PHP. If there is anything in viewFunctions.php that modifies the output of the $viewObject instance, consider making that into a class and have it aggregate the $viewObject into a property, e.g.

    // viewFunctions.php
    include_once 'ItemViewPackage.php';
    $viewObject = new ItemViewPackage($_GET['page']);
    $helper = new ViewObjectHelper($viewObject);
    

    then you can access the $viewObject from within the Helper object with $this->propertyName.

    As for reducing load to the database: this is a solved problem. Consider using a cache.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm making a simple page using Google Maps API 3. My first. One marker
I want to count how many characters a certain string has in PHP, but
I have a JSP page retrieving data and when single or double quotes are
Does anyone know how can I replace this 2 symbol below from the string

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.