1

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!

3
  • 2
    Are you accessing these two files in the same request? Or if you are not accessing them via http how are you calling them? Commented Jun 5, 2012 at 20:19
  • @NathanielFord I am not suite sure what you mean (once again, new to this). when I call ajax_show_timesheet, it is via ajax, so I guess....it is a different request? Commented Jun 5, 2012 at 21:11
  • So, AJAX is going to create a new request/response. That is handled separately by the web container. The container will treat it as a pristine process (so that two clients aren't using the same data illegally). I think there are ways to cache certain data, but it's not a good idea. Commented Jun 5, 2012 at 21:56

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

if I do this, it just prints out the contents of the array. a whole big mess of info
after reading about singletons on google, it seems like they are what I want to be using (I want one array which can be used by ANY .php file, so I only need to query the project database once). But it seems to me that the way I am doing IS like a singleton? On a sidenote, is this bad design?
So, in theory it would be like a singleton except that you're not in the same request/response. Singletons are per process, not per program. A program with a singleton running in two processes does not share that information. What you probably are looking for is something like memcached, which will let you store something and quickly retrieve it rather than querying the database again.
That stuff looks too yucky to me. I think I will just call Project::createProjects() whenever I make an ajax call...I am not making a huge system or anything, so it won't slow it down by a lot
That is true. Unless you're planning to scale it to thousands or more of clients, it's probably not a huge tax on the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.