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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:16:15+00:00 2026-06-05T06:16:15+00:00

I am pretty rusty on php, and am just learning how to use classes,

  • 0

I am pretty rusty on php, and am just learning how to use classes, and now something wierd is happening, which may just be how php works – I am not sure.

I have an array which is static to a class (Project), and what I want to do is fill the array as soon as the page is loaded (in index.php), and then use projects from it from it when populating data. But I also want to use the SAME project array in other php files (such as my ajax_show_timesheet.php).

When I try to access the array from another php file:

Project::$projectArray[key];

The array is empty. If I call

Project::createProjects();

again from within the other php file, it repopulates.

So why can’t I access the same static array from within various .php files?

Here is what I am doing:

CLASS PROJECT:

class Project
{

  public static $projectArray;

  public $projectID;

  public function __construct($projID=0){
    $this->projectID = $projID;

  }

  public static function createProjects(){

    $projectResult = mysql_query("SELECT * FROM $tblProjects");

    $numRows = mysql_numrows($projectResult); 
    $i = 0;

    while($i < $numRows){
      //for each project in the DB, add one to the array
      Project::$projectArray[mysql_result($projectResult, $i, "projectID")] 
             = new Project(mysql_result($projectResult, $i, "projectID"));

      $i++;
    }
  }
}

In index.php I fill initialize Project:

Project::createProjects();

In ajax_show_timesheet.php I do something like:

echo Project::$projectArray[key]->projectID

and I just get back nothing.

Any help on how to use static variables across multiple files would be great!

  • 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-05T06:16:17+00:00Added an answer on June 5, 2026 at 6:16 am

    What happens when you do this?

    public static function createProjects(){
    
        $projectResult = mysql_query("SELECT * FROM $tblProjects");
    
        $numRows = mysql_numrows($projectResult); 
        $i = 0;
    
        while($i < $numRows){
            //for each project in the DB, add one to the array
            Project::$projectArray[mysql_result($projectResult, $i, "projectID")] 
                  = new Project(mysql_result($projectResult, $i, "projectID"));
    
            $i++;
        }
        var_dump(Project::$projectArray);
    }
    

    Because this suggests that you can’t initialize your static variable in the way that you are. It seems like you’re trying to use Project like a singleton, but that is not how a php singleton works.

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

Sidebar

Related Questions

I'm pretty rust at my C still and I am just not figuring this
My C++ is pretty rusty so now that I started using it for a
Pretty straight forward. MSDN states that you can use ref, but not out for
Pretty sure there is an easy answer to this, but just can't find the
Pretty basic question, I know... I just don't know how it's done. I want
Pretty pointless but if i encrypt something with my own cypher (i'll assume it
I am a bit rusty at PHP, so bear with me. I am making
This is a pretty basic scenario but I'm not finding too many helpful resources.
Pretty new here. But before I ask my question, I'm not looking for teh
Pretty green on this. Any ideas why this works: $('.myclass div').append(' <a >HELLO WORLD</a>').click(this.function);

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.