以下是这个想法:我正在php中构建一个文件上传站点,其中用户的记录作为User类的对象数组存储在一个平面文件中。在User类的结构中,我将$ iUsers定义为静态变量,以便计算用户数,而无需单独的records_number文件。
实验:假设有一些第一位访客Joe注册,登录并执行操作。他的$ iUsers版本是1,因为他是唯一的用户。与此同时,其他游客也会注册。所以在Joe上传文件之后,ajax会触发update_user_activity函数,它会尝试读取records_file并更新它。
我的问题:从文件中读取对象的数组会发生什么? User :: $ iUsers是否保持不变,它是否获得文件的类静态变量(例如6,如果已经注册了5个访问者),或者获得了一些模糊的,未定义的值?
编辑: 这是所请求的类实现的示例:
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
}
}
}