Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class FileARModel
- {
- private $dataFile;
- protected function __construct()
- {
- $this->dataFile = get_class($this) . '.txt';
- }
- protected function save()
- {
- file_put_contents($this->dataFile, serialize($this));
- }
- public static function loadById($id)
- {
- $allData = file($this->dataFile);
- foreach($allData as $line)
- {
- $user = unserialize($line);
- if($user->getId() == $id)
- return $user;
- }
- return null;
- }
- }
- class User extends FileARModel
- {
- private $id = null;
- private $name;
- public function getId()
- {
- return $this->id;
- }
- public function getName()
- {
- return $this->id;
- }
- public function getName()
- {
- return $this->name;
- }
- public function setName($newName)
- {
- if($this->validateName($newName))
- $this->name = $newName;
- else
- throw new \Exception('Invalid name');
- }
- private function validateName($name)
- {
- return preg_match('#[a-z]*#iu', $name);
- }
- public function setId($id)
- {
- $id = intval($id);
- if($id <= 999999 || >= 999999999)
- throw new \Exception('invalid id');
- $this->id = $id;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement