PHP Accessing variables stored in an object in a class -
i have dived in php objects , trying set/get properties.
i think understanding getters , setters , magic methods, @ least pros conns of using them. (i'm using magic method right now, have less code maintain while learn, , not sure how many bits of info need store user)
i have $_userdata object set of properties. loggedin (true false) firstname(firstname of user) lastname (lastname of user) ....and others
i having problems setting , getting values
require_once('facebook/facebook.php'); // require facebook sdk require_once('config.class.php'); // require config class require_once('dbconn.class.php'); //require database connector class user{ public $_userdata; // holds information of our user private $conn; // our database connection public $facebook; // facebook sdk public $fbuser; // facebook user //constructor public function __construct(array $properties=array()) { $this->conn = dbconn::getinstance(); //get db instance $this->facebook = new facebook(config::read('fb.facebook')); $this->_userdata = $properties; } // magic methods reading/writing our user properties! public function __set($property, $value){ return $this->_userdata[$property] = $value; } public function __get($property){ return array_key_exists($property, $this->_userdata)? $this->_userdata[$property]: null ; } /** * getsession - saves session vars our user * @return bool: true - session retrieved * false - no session found */ public function getsession(){ if (isset($_session['loggedin']) && $_session['loggedin'] == true) { //save session user $this->_userdata->email = $_session['email']; $this->_userdata->loggedin = $_session['loggedin']; $this->_userdata->provider = $_session['provider']; $this->_userdata->uid = $_session['uid']; //something wrong properties dont set!!!! return true; }else{ return false; } } public function getprofile(){ //first set users profile information try{ $pdoquery = ("select email_upl as'email', firstname_upl 'firstname', lastname_upl 'lastname', username_upl 'username', image_upl 'image', info_upl 'info' userprofile_upl email_upl = :email" ); $stmt = $this->conn->dbh->prepare($pdoquery); //this->_userdata->email returns null... $stmt->bindparam(':email', $this->_userdata->email); $row = $stmt->fetch(pdo::fetch_obj); // row has no data because email property has no value!!! //var_dump($row); $this->_userdata->email = $row->email; $this->_userdata->firstname= $row->firstname; $this->_userdata->lastname = $row->lastname; $this->_userdata->info = $row->info; return (true); }catch(pdoexception $e){ echo('there error while connecting profile<br>'); return false; } } /** * loginfacebook - logs user site facebook * checks see if user has account , if not make one. * checks see if user has profile , if not make one. * checks see if user has facebook credentials on site * @return mixed */ public function loginfacebook(){ $fbuser = $this->facebook->getuser();//get facebook user if($fbuser){// if have user returned have user who's authenticated , logged in facebook try { // proceed knowing have user. $me = $this->facebook->api('/me'); // facebook user //generate log-out url $params = array( 'next' => config::read('url.logout')); //set user logout_url $this->_userdata->logout_url = $this->facebook->getlogouturl($params); //set users details // values not getting set // when setsession called has no values poulate session $this->_userdata->uid = $this->facebook->getuser(); $this->_userdata->firstname = $me['first_name']; $this->_userdata->lastname = $me['last_name']; $this->_userdata->email = $me['email']; $this->_userdata->provider = 'facebook'; var_dump($this->_userdata); if(!$this->fbaccountexists($this->_userdata)){ //$this->fbcreateaccount($this->_userdata); }; if(!$this->profileexists()){ //$this->createprofile(); } var_dump($this->_userdata); $this->setsession(); } catch (facebookapiexception $e){ //if theres error $fbuser = null; //set user nothing return false(); } } if(!$fbuser){ //get login url, returns page can process login($fblogin_url) $loginurl = $this->facebook->getloginurl(array('redirect_uri'=>config::read('url.fblogin'), false)); header('location: '.$loginurl);//redirect login page } } }
usage:
$user = new user(); // create new user $user->getsession(); //get user session var_dump($user->_userdata); // data contains null values
the session contains following data
["loggedin"]=> bool(true) ["email"]=> null ["uid"]=> null ["provider"]=> null }
the user object contains no data
$this->_userdata
array(0) { }
so problem lies within initial loginfacebook method not setting properties.
the setters not working there....
Comments
Post a Comment