PHP OOP-based login system -


lets building oop-based user authentication system, , incorporate following principles: direct injection, inheritance, encapsulation, polymorphism , single responsibility principle.

my background in programming has relied on procedural programming, , thus, finding difficult put these practices correct use.

assume have these classes:

class config {     public function set($key, $value);     public function get($key, $default = null); }  class user {     public function __construct(pdo $dbh, $id = null);     public function setprofile(profile $profile); }  class auth {     public function __construct(config $config);     public function login($username, $password, $keeploggedin = true);     public function isloggedin();     public function getloggedinuser();     public function logout();     public function register(array $data); }  class session {     public function start($sessionname = null);     public function write($key, $value);     public function read($key, $default = null); }  class profile {     public function setaddress(address $address);     public function setname($name);     public function setdob(datetime $date);     public function getage(); }  class validator {     public function validate($input); } 

i have intentionally left off function bodies keep things simple.

to best of knowledge, believe i'm using principles correctly. however, still unclear how connect classes like: validator user model, user model auth , session auth class. of depend on each other.

you on right track. way these classes connect each other called extending. tend go towards mvc setup, meaning model, view, controller.

your logic goes controller, db queries , concrete end methods go in model. controller receives requests , returns responses. it's middleman. talks end after request has been made it, , feeds front in via response.

so have core controller (keep bare minimal), each class make extends core controller. controller tie together.

 <?php //your main core controller, load these things need avilable, long class extended class corecontroller {      public $auth     public $session;     public $view;     function construct__ ()    {        $this->auth = instantiateauthclasshere();        $this->session = instantiatesessionclasshere();        $this->view = instantiateviewclasshere();    }      public function anotherhelperforsomething(){         //helper stuff method         } }  //index, page, or content controller, depending on how many need, i.e. if want controller each page, thats fine, e.g indexcontroller, etc.. //this middle man, has logic, receives requst, returns response view. class controller extends corecontroller {      public function index (){          $usermodel = new usermodel();          //do         $session = $this->session;          $content = 'some html';         $userinfo = $usermodel->getusers();          $view = $this->view->render( array(             'content' => $content,             'userinfo' => $userinfo,         ));          return $view;     } }  //core libraries class validator {     //your validator stuff }  //core libraries class session {     //your validator stuff }  //core libraries class auth {     //your validator stuff }  class coremodel{      public $validator;      function __construct(){         $this->validator = instantiatevalidatorclasshere();     } }  //a user model  class (back end). want model class each db table pretty much.  class usermodel extends coremodel {   // if need validator anywhere inside class, globally available here inside class extends coremodel, e.g.  $this->validator->methodname()       public function getusers (){         $sql = 'select * users';         $result = $db->get($sql);          return $result;     } } 

notice, on controller, generic name indexcontroller, or custom. also, have word extends there. inherits objects parent extends. inside it, available via $this->. see example $this->session.

try avoid constructs - don't need them anywhere except core, , under special circumstances, might need check before that. dont use constructs anymore. can bit clunky , unmanageable.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -