title: Advanced oop concept
body: <p>I am writing database api for my company project.</p>
  
  <p>we have 4 classes in database api.</p>
  
  <ol>
  <li>user class (include information about user )</li>
  <li>gib class (include gib information)</li>
  <li>card class(
  <ol>
  <li>each user owns one card </li>
  <li>each user owns two gib card </li>
  <li>one user can give card to other user any card user or gib card .</li>
  </ol></li>
  <li>connection 
  <ol>
  <li>there can be many connection of user card or gib card in particular gib.</li>
  </ol></li>
  <li>db class for making db query.</li>
  </ol>
  
  <p>This page where i am calling the methods of classes </p>
  
  <pre><code>&lt;?php
   include_once "config.php";
   foreach($_REQUEST as $key=&gt;$value){
       $$key=$value;
   }
  
  if($pagelength=='')$pagelength=10;// default page length
  //because by username can update the other users data 
  if($username!='' &amp;&amp; $command!='saveuser'){
      $user=User::getuserarray($username);
      $userid=$user['userid'];
  }
  if($isowner!=''){
      $user=User::getuserarray($isowner);
      $userid=$user['userid'];
  }
  $users = array (
      "userid"  =&gt; $userid,
      "username" =&gt; $username,
      "firstname"   =&gt; $firstname,
      "lastname"   =&gt; $lastname,
      "password"   =&gt; $password,
      "email"   =&gt; $email,
      "photo"   =&gt; '',
      "url"   =&gt; $url,
      "avatar_url"   =&gt; $avatar_url,
      "thumb"   =&gt; $thumb,
      "crop_url"   =&gt; $crop_url,
      "crop_position"   =&gt; $crop_position
  );
  $getcard=array(
      'cardid'=&gt;$cardid,
      'card_type'=&gt;$card_type,
      'status'=&gt;$status,
      'userid'=&gt;$userid,
      'userid_to'=&gt;$userid_to,
      'message'=&gt;$message,
      'name'=&gt;$name,
      'pagelength'=&gt;$pagelength,
      'pagenumber'=&gt;$pagenumber,
      'fields'=&gt;$fields
  );
  $getuser=array(
      'userid'=&gt;$userid,
      'name'=&gt;$name,
      'pagelength'=&gt;$pagelength,
      'pagenumber'=&gt;$pagenumber,
      'fields'=&gt;$fields
  
  );
  $getgib=array(
      'gibid'=&gt;$gibid,
      'userid'=&gt;$userid,
      'isowner'=&gt;$isowner,
      'description'=&gt;$description,
      'tagline'=&gt;$tagline,
      'name'=&gt;$name,
      'pagelength'=&gt;$pagelength,
      'pagenumber'=&gt;$pagenumber,
      'fields'=&gt;$fields
  );
  $getlink=array(
      'gibid'=&gt;$gibid,
      'view'=&gt;$view,
      'userid'=&gt;$userid,
      'cardid'=&gt;$cardid,
      'name'=&gt;$name,
      'pagelength'=&gt;$pagelength,
      'pagenumber'=&gt;$pagenumber,
      'fields'=&gt;$fields
  );
  switch($command){
      case 'login':
          $user=new User(array());
          $msg=$user-&gt;login($username,$password);
          break;
      case 'logout':
          $user=new User(array('userid'=&gt;$userid));
          $user-&gt;logout($id_session);
          break;
      //~ case 'isowner':
          //~ $gib=new Gib(array('gibid'=&gt;$gibid));
          //~ $user=new User(array('userid'=&gt;$userid));
          //~ if($user-&gt;validateUserid()!=true) return $msg;
          //~ if($gib-&gt;validateGibid()!=true) return $msg;
          //~ $msg=$gib-&gt;isowner($userid,$gibid);
          //~ break;
      case 'saveuser':
          $user=new User($users);
          if($userid==''){
              //checking if blank fields
              $msg=$user-&gt;validate();
              if($msg!='ok')break;
              $msg=$user-&gt;adduser();
          }else{
              $msg=$user-&gt;updateuser();
          }
          break;
      //~ case 'updateuser':
          //~ $user=new User($users);
          //~ $user-&gt;updateuser();
          //~ break;
      case 'changepassword':
          $user=new User($users);
          $msg=$user-&gt;changepassword($old_pswd,$new_pswd,$confirm_pswd);
          break;
      case 'changeimage':
          $user=new User($users);
          $user-&gt;changeimage($thumb,$url,$avatar_url,$crop_position,$crop_url);
          break;
  
      case 'getuser':
          $user=new User(array('userid'=&gt;$userid));
          $msg=$user-&gt;getuser($getuser);
          break;
      /*----------link command -----------*/
      case 'getlink':
          $connection=new Connection(array());
  
          $connections=$connection-&gt;getlink($getlink);
          $connections=array('links'=&gt;$connections);
          echo $msg=json_encode($connections);
          break;  
      case 'createlink':
          $user=new User($users);
          $msg=$user-&gt;validateUserid();
          if($msg!='ok') break;
          $connection=new Connection(array());
          $msg=$connection-&gt;createlink($getlink);
          break;
      case 'updatelink':
          $connection=new Connection(array('linkid'=&gt;$linkid));
          $msg=$connection-&gt;validate();
          if($msg!='ok')break;
          $connection-&gt;updatelinkposition($positionx,$positiony);
          break;  
      case 'deletelink':
          $connection=new Connection(array('linkid'=&gt;$linkid));
          $msg=$connection-&gt;validate();
          if($msg!='ok')break;
          $connection-&gt;deletelink();
          break;
      //~ case 'cardsend_validate':
          //~ $card=new Card(array('cardid'=&gt;$cardid));
          //~ $msg=$card-&gt;cardSendValidate($userid_to);
          //~ break;
      case 'getcard':
          $card=new Card(array('cardid'=&gt;$cardid));
           $cards=$card-&gt;getcardlist($getcard);
          if($cardid=='')
              echo $msg=$cards;
          else
              echo $msg=json_encode($cards);
          break;
      case 'givecard':
          $card=new Card(array('cardid'=&gt;$cardid));
          $msg=$card-&gt;validate();
          if($msg!='ok')break;
          $user=new User(array('userid'=&gt;$userid_to));
          $msg=$user-&gt;validateUserid();
          if($msg!='ok') break;
          $msg=$card-&gt;givecard($getcard);
          break;
      case 'cardar'://accept reject
          $card=new Card(array('cardid'=&gt;$cardid));
          $msg=$card-&gt;validate();
          if($msg!='ok')break;
          $msg=$card-&gt;acceptReject($getcard);
          break;
      case 'deletecard':
          $card=new Card(array('cardid'=&gt;$cardid));
          $msg=$card-&gt;validate();
          if($msg!='ok')break;
          $msg=$card-&gt;delete_card();
          break;
      case 'getgib':
          $gib=new Gib(array('gibid'=&gt;$gibid));
          $gibs=array('success'=&gt;true,'gibs'=&gt;$gib-&gt;getgibs($getgib));
          echo $msg=json_encode($gibs);
          break;
      case 'savegib':
          $gib=new Gib(array('gibid'=&gt;$gibid));
          $user=new User($users);
          $msg=$user-&gt;validateUserid();
          if($msg!='ok') break;
          if($gibid==''){
              $gibA=$gib-&gt;creategib($name,$type,$userid,$description,$tagline,$gib_background);
              $gibid=$gibA['gibid'];
              $gib-&gt;gibid=$gibid;
              $msg= 'gib created Successfully';
          }else{
              $gib-&gt;updategib($name,$description,$tagline,$gib_background);
              $msg= 'update Form submission complete';
          }
          $arrayjson1=array(
              'success' =&gt; true,
              'message'=&gt;$msg
          );
          $gibA=$gib-&gt;getDetail();
          echo $msg= formjson(array(),$gibA,$arrayjson1); 
          break;
  
  
      default:
          break;
  }
  
  ?&gt;
  
  &lt;script language="javascript"&gt;
  window.location.href="enterspace.php?msg=&lt;?php echo urlencode($msg); ?&gt;";
  &lt;/script&gt;
  </code></pre>
  
  <p>Here i am showing only you the user class for suggestion </p>
  
  <pre><code>&lt;?php
  Class User{
  
      var $userid;
      var $username;
      var $firstname;
      var $password;
      var $email;
      var $photo;
      var $avatar;
      var $thumbnail;
      var $crop_url;
      var $crop_position;
  
  
  
      function User($users){
          $this-&gt;userid=$users['userid'];
          $this-&gt;username= $users['username'];
          $this-&gt;firstname=$users['firstname'];
          $this-&gt;lastname=$users['lastname'];
          $this-&gt;password= $users['password'];
          $this-&gt;email=$users['email'];
          $this-&gt;photo= $users['photo'];
          $this-&gt;avatar= $users['avatar'];
          $this-&gt;thumbnail= $users['thumbnail'];
          $this-&gt;crop_url= $users['crop_url'];
          $this-&gt;crop_position= $users['crop_position'];
  
      }
      function validateUserid(){
          if($this-&gt;userid==''){
              $msg=geterrormsg(1);
              $arrayjson1=array(
                          'success' =&gt; false,
                          'message'=&gt;$msg,
                          'error_code'=&gt;'1');
              echo $msg= formjson(array(),array(),$arrayjson1);
              return $msg;
  
          }
          $sql="select count(*) from users where userid=?";
          $count=Db::getValue($sql,$this-&gt;userid);
          //User not exist
          if($count&lt;=0){
              $msg=geterrormsg(1);
              $arrayjson1=array(
                          'success' =&gt; false,
                          'message'=&gt;$msg,
                          'error_code'=&gt;'1');
              echo $msg= formjson(array(),array(),$arrayjson1);
              return $msg;
          }
          return 'ok';
  
      }   
  
      function validate(){
          //checking if blank fields
          if($this-&gt;firstname=='' or $this-&gt;lastname=='' or $this-&gt;password=='' or $this-&gt;email=='' or $this-&gt;username==''){
              $msg=geterrormsg(6);
              $arrayjson1=array(
                          'success' =&gt; false,
                          'message'=&gt;$msg,
                          'error_code'=&gt;'6'
                      );
              echo $msg= formjson(array(),array(),$arrayjson1);   
              return false;
          }
          if($this-&gt;userid==''){
              $totalUsers = Db::getValue('SELECT COUNT(username) FROM users where username = ?',$this-&gt;username);
              if($totalUsers&gt;0){
                  //username already exist
                  $msg=geterrormsg(5);
                  $arrayjson1=array(
                          'success' =&gt; false,
                          'message'=&gt;$msg,
                          'error_code'=&gt;'6'
                      );
                  echo $msg= formjson(array(),array(),$arrayjson1);
                  return $msg;
              }
              $totalUsers = Db::getValue('SELECT COUNT(email) FROM users where email = ?',$this-&gt;email);
              if($totalUsers&gt;0){
                  $msg=geterrormsg(4);
                      //email already exist
                  $arrayjson1=array(
                          'success' =&gt; false,
                          'message'=&gt;$msg,
                          'error_code'=&gt;'4'
                      );
                  echo $msg= formjson(array(),array(),$arrayjson1);   
                  return $msg;
              }
          }
          return 'ok';
      }
      function login($loginUsername,$loginpassword){
          //query for checking user username  exist or not 
          $sql="select count(*) from users where username=?";
          $count=Db::getValue($sql,$loginUsername);
          if($count&lt;=0){
              $msg=geterrormsg(1);
              $arrayjson1=array(
                          'success' =&gt; false,
                          'message'=&gt;$msg,
                          'error_code'=&gt;'1'
                      );
              echo $msg= formjson(array(),array(),$arrayjson1);   
              return $msg;
          }
          $sql="select AES_DECRYPT(password,'text') as password,userid from users where username=?";
          $row=Db::getRow($sql,$loginUsername);
          if(is_array($row))  extract($row);
          $this-&gt;userid=$userid;
          //for checking password
          if($loginpassword != $password){
              $msg=geterrormsg(2);
              $arrayjson1=array(
                          'success' =&gt; false,
                          'message'=&gt;$msg,
                          'error_code'=&gt;'2'
                      );
              echo $msg= formjson(array(),array(),$arrayjson1);   
              return $msg;
          }
  
          //query for checking user exist  in session table with status =1
  
          //~ $ses_id = session_id(); 
          //~ $_SESSION['username']=$loginUsername;
          //~ $_SESSION['userid']=$userid;
          //~ $sql="DELETE FROM sessions WHERE userid=? or id_session=?";
          //~ Db::execute($sql,array($userid,$ses_id));
              //~ $sql="INSERT INTO sessions  (id_session,    userid,     START, 
          //~ END,    STATUS,     last_update )   VALUES  (?, ?,current_timestamp,    'end',  '1',    current_timestamp   );";
          //~ Db::execute($sql,array($ses_id,$userid));       
  
          $gibid=$this-&gt;getDefaultGibId();        
          //making user array
          $row=$this-&gt;getDetail();
          unset($row['updatedon'],$row['createdon']);
  
          $gib=new Gib(array('gibid'=&gt;$gibid));
          //~ $gibs=$gib-&gt;getgibs($userid,'',5);
          $systemgibid=$gib-&gt;systemgibid();
          $arrayjson1=array(
                      'success' =&gt; true,
                      'message'=&gt;'User logged in successfully',
                      'gibid'=&gt;$gibid,
                      'systemgibid'=&gt;$systemgibid
                  );
          echo $msg= formjson(array(),$row,$arrayjson1);  
          return $msg;    
  
      }
      function getDefaultGibId(){
          $sql="SELECT referid FROM cards WHERE userid_from=? AND userid_to=?  and card_type='A' " ;
          $gibid=Db::getValue($sql,array($this-&gt;userid,$this-&gt;userid));
          return $gibid;
      }
  
      //making user array
      function getDetail(){
          $sql="select  * from users where userid=?";
          $row=Db::getRow($sql,$this-&gt;userid);
          unset($row['password']);
          return $row;
      }
      // for creating new user 
      // will have one user entry , one profile card entry in card table , one system gib 
      function adduser(){
           $sql = "INSERT INTO users  (username, firstname,   lastname, PASSWORD,email,createdon  )
              VALUES  (?, ?, ?, AES_ENCRYPT(?,'text'),?,current_timestamp);";
          Db::execute($sql,array($this-&gt;username,     $this-&gt;firstname, $this-&gt;lastname,$this-&gt;password,$this-&gt;email));
          $this-&gt;userid=Db::getLastInsertId();
          //make profile card in cards table...
          $sql="INSERT INTO cards( userid_from, userid_to,card_type, referid,status,createdon)VALUES(?, ?, ?, ?, ?,current_timestamp)";   
          Db::execute($sql,array($this-&gt;userid,$this-&gt;userid,'V',$this-&gt;userid,'A'));
  
          $id_card=Db::getLastInsertId();
          $gib=new Gib(array());
          $systemgibid=$gib-&gt;systemgibid();
          //make system gib card in cards table...
          $sql="INSERT INTO cards(userid_from,    userid_to,  referid, 
              card_type   ,status,createdon)VALUES(?,  ?, ?,?,?,current_timestamp )"  ;   
          Db::execute($sql,array($this-&gt;userid,$this-&gt;userid,$systemgibid,'A','A'));  
          $this-&gt;firstname=$this-&gt;firstname."'s Gib";
          //create gibs define in connection.php type D for default gib
          $gib-&gt;creategib($this-&gt;firstname,'D',$this-&gt;userid,'','');
          $arrayjson=array();
          $row=$this-&gt;getDetail();
  
          $arrayjson1=array(
              'success' =&gt; true,
              'message'=&gt;'Registered in successfully',
              'username'=&gt;$this-&gt;username
          );
          echo $msg= formjson($arrayjson,$row,$arrayjson1);   
          return $msg;
      }
      function updateuser(){
          $sql="UPDATE users SET firstname = ?,   lastname = ?, email=?   WHERE userid = ? ";
          Db::execute($sql,array($this-&gt;firstname,$this-&gt;lastname,$this-&gt;email,$this-&gt;userid));
          $user=$this-&gt;getDetail();
          $arrayjson1=array(
              'success' =&gt; true,
              'message'=&gt;'form submission complete'
          );
          echo $msg= formjson(array(),$user,$arrayjson1); 
          return $msg;
      }
      function changepassword($old_pswd,$new_pswd,$confirm_pswd){
  
          $sql="select count(*) from users where password=AES_ENCRYPT(?,'text') and userid=? ";
          $count=Db::getValue($sql,array($old_pswd,$this-&gt;userid));
          if($count&lt;=0){
              $msg=geterrormsg(28);
              //old passwod not matched
              $arrayjson1=array(
                  'success' =&gt; false,
                  'message'=&gt;$msg,
                  'error_code'=&gt;'28'
              );
              echo $msg= formjson(array(),array(),$arrayjson1);   
              return $msg;
          }
          //checking  confirm and new password 
          if($new_pswd!=$confirm_pswd){
              $msg=geterrormsg(29);
              $arrayjson1=array(
                  'success' =&gt; false,
                  'message'=&gt;$msg,
                  'error_code'=&gt;'29'
              );
              echo $msg= formjson(array(),array(),$arrayjson1);   
              return $msg;
          }
          $sql="UPDATE users  SET PASSWORD = AES_ENCRYPT(?,'text')
              WHERE userid = ? ; ";
          Db::Execute($sql,array($new_pswd,$this-&gt;userid));
          $arrayjson1=array(
              'success' =&gt; true,
              'message'=&gt;'password changed successfully .',
              'userid'=&gt;$this-&gt;userid
          );
          echo $msg= formjson(array(),array(),$arrayjson1);   
          return $msg;
      }
      function changeimage(){
  
          $sql="UPDATE users SET  thumbnail=? ,url=?, avatar=?,crop_position=?,   crop_url=?  WHERE userid = ? ";
          Db::Execute($sql,array($this-&gt;thumbnail,$this-&gt;url,$this-&gt;avatar,$this-&gt;crop_position,$this-&gt;crop_url,$this-&gt;userid));
          $user=$this-&gt;getDetail();
          $arrayjson1=array(
              'success' =&gt; true,
              'message'=&gt;'form submission complete'
          );
          echo $msg= formjson(array(),$user,$arrayjson1);     
      }
      function logout($id_session){
          //~ $sql="delete from sessions where userid=? and id_session=?";
          //~ Db::Execute($sql,array($this-&gt;userid,$id_session));
  
          //~ $sql="delete from occupants where userid=?";
          //~ Db::Execute($sql,$this-&gt;userid);
  
          //~ unset($_SESSION['username']);
          //~ session_destroy(); // start up your PHP session! 
  
  
          //~ echo $msg= "{'success': 'true','message':'logout successfully','userid':'$userid'}";
          return $msg;
      }
      function getuser($getuser){
          $userid=$getuser['userid'];
          $name=$getuser['name'];
          $pagelength=$getuser['pagelength'];
          $pagenumber=$getuser['pagenumber'];
          $fields=$getuser['fields'];
  
  
          $condition = '';
          $query=array();
  
          if($userid !='') {
              $condition .= " and userid=? ";
              array_push($query,$userid);
          }
          if($name!=''){
              $condition .= " and concat_ws('  ',firstname,lastname)  like ?";
              array_push($query,"%".$name."%");
          }
  
          if($pagelength!=''){
              $limitpagelength="limit $pagelength";
          }
  
          if($pagenumber!=''){
              $pagenumber=$pagelength*($pagenumber-1);
              $pagenumber="offset $pagenumber";
          }
          if($fields=='*'){
              $fields=",users.*";
          }elseif($fields!=''){
              $fields=",".$fields;
          }
  
  
          $sql="select  userid,concat_ws(' ',firstname,lastname) as name $fields 
              from users where 1=1   $condition order by updatedon $limitpagelength $pagenumber";
          $row=Db::getResult($sql,$query);
  
          $user=array();
          for($i=0;$i&lt;count($row);$i++){
              unset($row[$i]['password']);
              extract($row[$i]);
              $this-&gt;userid=$userid;
              $row[$i]['gibid']=$this-&gt;getDefaultGibId();
              array_push($user,$row[$i]);
          }
  
  
          $arrayjson1=array(
              'success' =&gt; true,
              'message'=&gt;'User data successfully',
              'users'=&gt;$user
          );
          echo $msg= formjson(array(),array(),$arrayjson1);   
          return $msg;
      }
      //making user array
      function getuserarray($username){
          $sql="select  * from users where username=?";
          $row=Db::getRow($sql,$username);
          unset($row['password']);
          return $row;
      }
  
  
  }
  
  
  ?&gt;
  </code></pre>
  
  <p>Please suggest me how can i improve my code by implementing advanced and all  oops concepts.
  If you find this question vague , please suggest the improvement.
  Thanks </p>