我刚刚潜入php对象并试图设置/获取一些属性。
我认为我理解吸气剂和制定者以及魔术方法,至少是专业人士使用它们。 (我现在正在使用一种神奇的方法,所以在我学习的时候,我需要维护更少的代码,而且我也不确定我需要存储多少关于我的用户的信息)
我想将$ _userdata作为具有一组属性的对象。 loggedin(true false) firstname(用户的名字) lastname(用户的姓氏) ....和其他人
我在设置和获取值时遇到问题
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; // the 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 for 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 to our user
* @return bool: true - session retrieved
* false - no session found
*/
public function getSession(){
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
//save session to this user
$this->_userData->email = $_SESSION['email'];
$this->_userData->loggedin = $_SESSION['loggedin'];
$this->_userData->provider = $_SESSION['provider'];
$this->_userData->uid = $_SESSION['uid'];
//something wrong as the properties dont get set!!!!
return true;
}else{
return false;
}
}
public function getProfile(){
//first set the users profile information
try{
$pdoQuery = ("SELECT
email_upl as'email',
firstname_upl as 'firstname',
lastname_upl as 'lastname',
username_upl as 'username',
image_upl as 'image',
info_upl as 'info'
FROM userprofile_upl
WHERE 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 the 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 was an error while connecting to your profile<br>');
return false;
}
}
/**
* loginFacebook - Logs a user into the site with facebook
* Checks to see if user has an account and if not make one.
* Checks to see if user has a profile and if not make one.
* Checks to see if user has facebook credentials on this site
* @return mixed
*/
public function loginFacebook(){
$fbuser = $this->facebook->getUser();//get facebook user
if($fbuser){// if we have a user returned we have user who's authenticated and logged in with facebook
try { // Proceed knowing we have a user.
$me = $this->facebook->api('/me'); // the facebook user
//generate a log-out url
$params = array( 'next' => config::read('url.logout'));
//set user logout_url
$this->_userData->logout_url = $this->facebook->getLogoutUrl($params);
//set the users details
// this values are not getting set
// so when the setSession is called it has no values to poulate the session with
$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 an error
$fbuser = null; //set the user to nothing
return false();
}
}
if(!$fbuser){
//get a login url, returns to this page so we can process the login($fblogin_url)
$loginUrl = $this->facebook->getLoginUrl(array('redirect_uri'=>Config::read('url.fblogin'), false));
header('Location: '.$loginUrl);//redirect to the login page
}
}
}
用法:
$user = new user(); // create a new user
$user->getSession(); //get the user from the session
var_dump($user->_userData); // data contains null values
会话包含以下数据
[ “的loggedIn”] =&GT;布尔(真) [ “电子邮件”] =&GT;空值 [ “UID”] =&GT; NULL [“provider”] =&gt; NULL}
用户对象不包含数据
$这 - &GT; _userData
array(0){}
所以问题在于初始loginFacebook方法没有设置属性。
安装员不在那里工作....