php - Best practices accessing a logged in User object from other classes -



php - Best practices accessing a logged in User object from other classes -

this question regarding best practice storing , accessing object class.

i'm using simple homemade mvc paradigm in php, class called user , has own methods , vars works database abstraction layer.

this class instantiated calling newuser($userid) retrieves info database given $userid or throws exception if there no user id.

every page has own webviewcontroller class governing content of page, , in instances page needs phone call $loggedinuser dependent functions such webviewcontroller->displayuserfriends(), might this:

<?php class webviewcontroller extends wvctemplate { // class vars , methods // ... public function displayuserfriends() { foreach($loggedinuser->getfriends() $friend) { // // ... } } } ?>

is there (best practice compliant) way store loggedinuser sort of global object, accessed within of class or webviewcontroller without instantiating within every class it's used?

the elegant solution problem utilize dependency injection. may need current user in multiple controllers best create authenticationservice (or similar) provides methods check whether user logged in , logged in user , encapsulates mutual functionality. can inject service instance in controllers need it.

there several standalone php dependency injection libraries out there:

php-di pimple aura.di dice

php mysql session pdo

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -