Here's the idea: I'm building a file upload site in php where the records of users are stored in a flat file as an array of objects of class User. In the structure of the User class i have defined $iUsers as a static variable so as to count the number of users, without the need to have a separate records_number file.
The experiment: Suppose some first visitor Joe registers, logs in and does stuff. His version of $iUsers is 1 since he is the only user. Meanwhile other visitors register as well. So after Joe has uploaded a file, an update_user_activity function is triggered by ajax, which will attempt to read the records_file and update it.
My question: What happens when the objects' array is read from the file? Does User::$iUsers remains unchanged, does it gets the file's class static variable (for example 6, if 5 more visitors have registered), or gains some obscure, undefined value?
EDIT: here is a sample of the class implementation as requested:
class User {
// user details, username, password, e-mail
static private $iUsers=0;
public function __construct($usr, $pwd, $eml) {
//initializes user details
$iUsers++;
}
// reads records from records file and locks the records file
static private function read_records(&$arr) {
//lock file?
//if User::check_connected()(checks session var) update $arr for the current user
//open REC_FILE
//unserialize to $arr
//close REC_FILE
}
// Updates records from records file to the given array
static private function update_records(&$arr) {
//unlock file
//open REC_FILE
//serialize $arr to file
//close REC_FILE
}
static private function unlock_records() {
//unlock file
}
// update function
private function update_user_activity() {
read_records($recs);
foreach($recs as $cur) {
if($cur->sUsername === $this->sUsername) {
//stuff here (updates the $cur
update_records($recs);
return true;
}
}
unlock_records();
return false;
}
static public function register(&$usr, &$pwd, &$eml) {
//$recs array of records
read_records($recs);
if(iUsers<MAX_USERS) {
// loop through the records --- unlocks_records and returns error if same name or e-mail is found
$newuser = new User($usr, $pwd, $eml);
array_push($recs, $newuser);
update_records($recs);
return "success";
}
else {
unlock_records();
return "No capacity for new users!";
}
}
// login function
static public function login(&$usr, &$pw) {
//$recs array of records
read_records($recs);
if(User::$iUsers>0) {
foreach($recs as $cur) {
if($cur->check_matching($usr, $pw)) {
if($cur->$bActivated) {
// logged in
// stuff
update_records($recs);
return $cur; //return whatever is going to be stored in _SESSION array;
}
else {
unlock_records();
return "Your account is deactivated!";
}
}
}
unlock_records();
return "The username or password provided is wrong.";
}
else {
unlock_records();
return "There are no users registered!"; // no users registered
}
}
}