Hello. There is such a user class, for authorization, registration and checking whether the usver is authorized. I need to remake a class into functions, that is, call them as functions and not class objects. How can this be cut and assembled correctly?

class Account { private $account_data = []; public function __construct() { global $db; if(isset($_COOKIE['account_session'])) { $account_session = unserialize($_COOKIE['account_session']); if(isset($account_session['account_id']) && $account_session['token']) { $stmt = $db->prepare('SELECT * FROM `accounts` WHERE `id` = :id AND `token` = :token'); $stmt->bindParam(':id', $account_session['account_id'], PDO::PARAM_INT); $stmt->bindParam(':token', $account_session['token'], PDO::PARAM_STR); $stmt->execute(); if($stmt->rowCount()) { $this->account_data = $stmt->fetch(); } else { setcookie('account_session', null, 1, '/'); } } else { setcookie('account_session', null, 1, '/'); } } } public function Auth() { return isset($this->account_data['id']); } public function getData($id = 0) { global $db; if($id && $id != @$this->account_data['id']) { $stmt = $db->prepare('SELECT * FROM `accounts` WHERE `id` = :id'); $stmt->bindParam(':id', $id, PDO::PARAM_INT); $stmt->execute(); return $stmt->fetch(); } else { return $this->account_data; } } 

}

  • one
    why do you need it? - Edward
  • I do not know how to work with classes yet, I am writing on the procedure. - Vadim Romashenko
  • in classes, the same functions with variables as in the procedure, they are called only slightly differently. Study in parallel and OOP, believe me - without it, now you will not find work. - Edward

1 answer 1

Extremely doubtful request. In my opinion, if you had the opportunity to work with classes, you need to learn =)

But here is your answer (I tried to keep the style in which your question was written):

  <?php $db = ''; // you database // init data $account_data = init($db); // init global account_data // get data $id = 0; $getData = getData($id, $account_data); //check fo auth $auth = auth($account_data); function init($db) { if (!isset($_COOKIE['account_session'])) { return null; } $account_session = unserialize($_COOKIE['account_session']); if (isset($account_session['account_id']) && $account_session['token']) { $stmt = $db->prepare('SELECT * FROM `accounts` WHERE `id` = :id AND `token` = :token'); $stmt->bindParam(':id', $account_session['account_id'], PDO::PARAM_INT); $stmt->bindParam(':token', $account_session['token'], PDO::PARAM_STR); $stmt->execute(); if ($stmt->rowCount()) { return $stmt->fetch(); } setcookie('account_session', null, 1, '/'); return null; } setcookie('account_session', null, 1, '/'); return null; } function auth($account_data) { return isset($account_data['id']); } function getData($id = 0, $account_data) { if ($id && $id != @$account_data['id']) { $stmt = $db->prepare('SELECT * FROM `accounts` WHERE `id` = :id'); $stmt->bindParam(':id', $id, PDO::PARAM_INT); $stmt->execute(); return $stmt->fetch(); } return $account_data; }