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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:38:25+00:00 2026-06-11T00:38:25+00:00

I have an Object that needs to access a previously declared array in my

  • 0

I have an Object that needs to access a previously declared array in my statistics. I can of course create the entire array inside of the object, but since multiple objects use the exact same array there is no reason to clog up memory or time by making a call to the Database to create the same array every time I create a new object.

So, I understood that Objects cannot access global variables, but is there any work-around to access an external Array from within the object?

example Code:

global $stats = array();
       $stats[1]['value']= 10;
       $stats[1]['value1'] =2;
       $stats[2]['value']= 12;
       $stats[2]['value1'] =1;

class Obj() {

  private $valueA;
  private $valueB;

  function __construct($user) {
    //access Database lets call $SQL;
    $valueA = SQL->value;

  }

  function showA() {
    return ( $valueA * $stats[1]['value1']) + $stats[1]['value'];
  }

}
  • 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-06-11T00:38:27+00:00Added an answer on June 11, 2026 at 12:38 am

    Thanks to Mike, Sheikh and Kristian,

    I can’t in all Faith give a tick to your answers, because your words did not help me to understand the answer, Putting ‘global $stats;’ into the class results in an Error which I pointed out in my responses. but I will ‘up’ your scores when I permission from the site to do so.

    For anyone looking for the answer to this, a Reminder, the key point is not to store the entire Array in the class, creating a huge waste of memory. The Key point is to gain access to the Variable which exists outside of the class.

    While adding access to the global $stats by including it in the functions of the class, does produce the required results, It still requires that the Data is being stored in the class, which is again, against the point. Sorry I wasn’t clear on this from the very beginning.

    Instead:

    example Code:

     function showA(&$stats) {
       return ( $valueA * $stats[1]['value1']) + $stats[1]['value'];
     }
    

    }

    This, if I understand correctly, will use the pointer to the $stats variable, only within the scope of returning the $valueA after it has been modified using the stats array. not copying the entire array into another memory location, nor the class.

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

Sidebar

Related Questions

When you have a domain object that needs to display as an interface control,
Let's say i have a griffon object that needs to be part of the
In an embedded program I have a screen object that needs to manage a
I have a current Date object that needs to be incremented by one day
Here is the scenario: I have an object called a Transaction that needs to
I have a list of object that need to be displayed and moveable on
I have a pretty large object that I need to pass to a function
I have a nested JSON object that I need to loop through, and the
If I have an object that holds the parameters for my method. I need
I have a simple Dictionary(of String, Object) that I need to iterate through and

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.