class - OOP PHP + PDO - Is this how it should be? -
i'm trying create simple example class before start implementing following projects , know if there could/should improve. glad if gives me input of how doing now.
simple class example:
// define class class user{ private $userid; private $username; private $email; function setuserid($newuserid){ $this -> userid = $newuserid; } function getuserid(){ return $this -> userid; } function setusername($newusername){ // update object $this -> username = $newusername; } function getusername(){ return $this -> username; } function setemail($newemail){ $this -> email = $newemail; } function getemail(){ return $this -> email; } public function saveuser(){ // check if user exists $user = new user(); if ($user->userexists($this->userid)){ // user exists - update him $pdo = new pdo('mysql:host=localhost;dbname=cms;charset=utf8', 'xxx', 'xxx'); $query = $pdo->prepare("update user set username = :username, email = :email, userid = :userid userid = :userid"); $query->bindvalue(':username', $this->username); $query->bindvalue(':email', $this->email); $query->bindvalue('userid', $this->userid); $query->execute(); $pro = null; } else{ // insert new $pdo = new pdo('mysql:host=localhost;dbname=cms;charset=utf8', 'xxx', 'xxx'); $query = $pdo->prepare("insert user (username, email) values (:username, :email)"); $query->bindvalue(':username', $this->username); $query->bindvalue(':email', $this->email); $query->execute(); // close connection $pdo = null; } } private function userexists($userid){ // returns true if users exists, false if not $pdo = new pdo('mysql:host=localhost;dbname=cms;charset=utf8', 'root', 'vertrigo'); $query = $pdo->prepare("select * user userid=:userid"); $query->bindvalue(':userid', $userid); $query->execute(); $row = $query->fetch(pdo::fetch_assoc); $pdo = null; // user if ($row){ return true; } else{ return false; } } } function getuserinfo($userid){ // open pdo $pdo = new pdo('mysql:host=localhost;dbname=cms;charset=utf8', 'root', 'vertrigo'); $query = $pdo->prepare("select * user userid=:userid"); $query->bindvalue(':userid', $userid); $query->execute(); $row = $query->fetch(pdo::fetch_assoc); $pdo = null; // userinfo $userinfo = new user(); $userinfo -> setuserid($userid); $userinfo -> setusername($row["username"]); $userinfo -> setemail($row["email"]); // return userinfo return $userinfo; }
this basic example of how work it. think should improve?
any php application have connect database once.
thus, class have use created connection, not create every time.
class user{ private $userid; private $username; private $email; private $db; function __construct($pdo) { $this->db = $pdo; } private function userexists($userid) { $sql = "select 1 user userid=?"; $stm = $this->db->prepare($sql); $stm->execute(array($userid)); return $stm->fetchcolumn(); } } // rest going use same approach } $pdo = //see pdo tag wiki theproper example $user = new user($pdo); if ($user->userexists($userid)) { // whatever }
Comments
Post a Comment