PHP新手,特别是OOP。
我有一个User.php类,我在login.php中使用。
$vars = $user->login($email, $pass)
目前我正在调用登录方法,最后我将调用if语句进行验证等。 然而,目前,我正在尝试连接到数据库,提取一些信息并将该信息添加到我班级的属性中。我可以将信息拉出来(通过方法login()中的对象的var_dump验证($ results的vardump),但由于某些原因,我的当前代码无法更新我的类属性。
这是我的代码
user.php的
<?php
class User {
public $id, $password, $first_name, $last_name;
private $user_level;
protected static $db_fields = array('id', 'first_name', 'last_name', 'pass');
protected static $table_name="users";
public function login($email, $pass) {
global $database;
$sql = "SELECT user_id, first_name, last_name, user_level FROM users WHERE (email='$email' AND pass=SHA1('$pass')) AND active IS NULL LIMIT 1";
$results = self::find_by_sql($sql);
if (!empty($results)) {
$this->setuservars($results);
return array_shift($results);
} else {
return false;
}
// return !empty($results) ? array_shift($results) : false;
}
private function setuservars($uservariables) {
$this->id = $uservariables->id;
$this->first_name = $uservariables->first_name;
$this->last_name = $uservariables->last_name;
$this->user_level = $uservariables->user_level;
}
public static function find_by_sql($sql="") {
global $database;
$results_array = $database->query($sql);
$object_array = array();
while ($row = $results_array->fetch_assoc()) {
$object_array[] = self::instantiate($row);
}
return $object_array;
}
public function mysqli_array_escape($arg1){
global $database;
foreach ($arg1 as $key => $value) {
$arg1[$key] = $database->real_escape_string($value);
}
return $arg1;
}
private static function instantiate($record) {
// Could check that $record exists and is an array
$object = new self;
foreach($record as $attribute=>$value){
if($object->has_attribute($attribute)) {
$object->$attribute = $value;
}
}
return $object;
}
private function has_attribute($attribute) {
return array_key_exists($attribute, $this->attributes());
}
protected function attributes() {
// return an array of attribute names and their values
$attributes = array();
foreach(self::$db_fields as $field) {
if(property_exists($this, $field)) {
$attributes[$field] = $this->$field;
}
}
return $attributes;
}
}
$user = new User();
?>
,这是我的login.php(我已经编辑了if语句以验证用户是否已成功登录,我已用“if(1 == 1){”语句替换以帮助调试代码。
if (isset($_POST['submitted'])) {
$postdata = $user->mysqli_array_escape($_POST);
//var_dump($user->results);
if (((!isset($_POST['email'])) || ($_POST['email']) == '') || (!isset($_POST['pass']) || ($_POST['pass']) == '') ) {
//error handling eventually
} else {
$email = $_POST['email'];
$pass = $_POST['pass'];
$vars = $user->login($email, $pass);
echo $vars->first_name;
if (1 == 1) {
echo "you have successfully logged in";
var_dump($user->id);
} else {
echo "not logged in";
}
}
}
哦,我收到的当前错误是“第26行脚本'F:\ internet \ www \ htdocs \ blissoop \ classes \ User.php'发生错误:试图获取非对象的属性 日期/时间:2012年4月13日05:01:09“
答案 0 :(得分:0)
我已在此问题的帮助下解决了这个问题
这是帮助的代码:Get value from Multidimentional Array containing an Object。
我给答案提供了+1的帮助。
foreach ($array as $item) {
$userId = $item->user_id;
//do something with the userId for this item
}
我必须遍历数组才能获得对象的属性。