/ Published in: PHP
I created this code for practice, and I looking for better and clear solution for the last instance where I can write or delete the data, but after I want to get back the content as array or the key as string. I want to use this write-read back with only one instance.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// The input is a textfile: // key1=val1 // key2=val2 // key3=val3 // key4=val4 // . // . // . // The class: class UserID { private $UIDPath; private $UserUIDcontent; public function __construct($UserID, $Action, $UID_KEY, $UID_VAL, $Back) { $UID_Content = $this -> UIDPath = 'the/path/to/input/text.txt'; } if ($Action == 'Read') { if ($UID_KEY == NULL) { $this -> UserUIDcontent = $UserUIDcontent; } else { $this -> UserUIDcontent = $UserUIDcontent[$UID_KEY]; } else { if ($UID_KEY == 'User') { return NULL; } else { echo 'ERROR: The search key: ' . $UID_KEY . ' not available in Class: ' . __METHOD__; return; } } } } elseif ($Action == 'Write') { } else { return; } } else { $UserUIDcontent[$UID_KEY] = $UID_VAL; } foreach ($UserUIDcontent as $key => $value) { if ($value !== NULL) { } } if ($Back != NULL) { return $this -> $UserUIDcontent = $UserUIDcontent[$Back]; } else { return $this -> $UserUIDcontent = $UserUIDcontent; } } else { return $this -> $UserUIDcontent = NULL; } } else { echo 'ERROR: The second parameter ' . $Action . ' not allowed in Class: ' . __METHOD__; return; } } else { echo 'ERROR: Required data source not found for Class: ' . __METHOD__; return; } } public function result() { return $this -> UserUIDcontent; } } // --------------------------------- // instances: // --------------------------------- $UserData = new UserID($UserID, 'Read', NULL, NULL, NULL); $array = $UserData -> result(); // Read full array $val = $UserData -> result()[key2]; // Read one data $UserData = new UserID($UserID, 'Read', 'key2', NULL, NULL); $val = $UserData -> result(); // Read one data $UserData; // write or delete more than one data by array $UserData = new UserID($UserID, 'Write', 'key2', NULL, NULL); $UserData; // write or delete one data // this is the problem: