2013-03-13 123 views
1

这是让我的大脑受伤的原因。首先,我知道刚刚足够的PHP在大约5分钟内解释它。然而,我非常精通面向对象编程。情况:实例化后PHP对象属性为空

我想建立一个php应用程序,作为一个intranet唯一的工具使用。我的想法是将当前用户保存在会话中。我创建了一个我的数据库用户表的PHP模型。在代码中,我对用户进行了身份验证,如果用户进行了身份验证,那么我将创建一个User对象并将其保存在会话中。问题是User对象被创建,但所有的属性都是空的。我已经研究了这里和PHP网络的一些答案,并尝试了几件事情,但问题依然存在。我不认为这是PHP相关的,我绝对认为这是我做错了,但只是不能指责它。这是来自登录脚本的相关代码示例。为了得到这里密码必须确认,变量$users是从我的数据库控制器中的函数直接返回。由于该变量包含在var_dump中,因此可能不需要返回该变量的代码。我曾尝试将相关的数组元素分别传递给构造函数,我试过传递完整的数组,我已经使用了serializeunserialize。我试图用$this->property = $someValue来设置它们。

if ($hash) { 
    $current_user = new User($users); 
    $_SESSION['current_user'] = serialize($current_user); 
    $current_user = unserialize($_SESSION['current_user']); 
    var_dump(session_id()); 
    var_dump($users); 
    var_dump($current_user); 
    var_dump($_SESSION['current_user']); 
} 

这里是我的构造函数:

class User { 
public $userid; 
public $fname; 
public $lname; 
public $email; 
public $username; 
public $function; 
public $d_joined; 
public $is_internal; 
public $is_active; 

function __costruct() { 
    $arguments = func_get_args(); 
    if (!empty($arguments)) { 
     foreach ($arguments[0] as $key => $property) { 
      if ($property_exists($this, $key)) { 
       $this -> {$key} = $property; 
      } 
     } 
    } 
} 

我试图_construct($args)_construct(array($args))并没有什么。这里是var_dump()的输出调用

string 'cgotecrpu7soqvepoimjo2s116' (length=26) 

array (size=1) 
0 => 
array (size=20) 
    'userid' => string '1' (length=1) 
    0 => string '1' (length=1) 
    'fname' => string 'First Name' (length=4) 
    1 => string 'First Name' (length=4) 
    'lname' => string 'Last Name' (length=8) 
    2 => string 'Last Name' (length=8) 
    'email' => string 'email address' (length=21) 
    3 => string 'email address' (length=21) 
    'username' => string 'username' (length=9) 
    4 => string 'username' (length=9) 
    'password' => string 'hashed password:salt' (length=65) 
    5 => string 'hashed password:salt' (length=65) 
    'function' => string '4' (length=1) 
    6 => string '4' (length=1) 
    'd_joined' => string '2013-01-14' (length=10) 
    7 => string '2013-01-14' (length=10) 
    'is_internal' => string '1' (length=1) 
    8 => string '1' (length=1) 
    'is_active' => string '1' (length=1) 
    9 => string '1' (length=1) 

object(User)[2] 
public 'userid' => null 
public 'fname' => null 
public 'lname' => null 
public 'email' => null 
public 'username' => null 
public 'function' => null 
public 'd_joined' => null 
public 'is_internal' => null 
public 'is_active' => null 

string 'O:4:"User":9{ 
s:6:"userid";N; 
s:5:"fname";N; 
s:5:"lname";N; 
s:5:"email";N; 
s:8:"username";N; 
s:8:"function";N; 
s:8:"d_joined";N; 
s:11:"is_internal";N; 
s:9:"is_active";N;}' (length=162) 

因此,从我所看到的,最初的阵列$users有正确的数据。 User对象在变量$current_user中创建。但是,当我尝试访问对象属性时,出现有关从非对象访问对象属性的错误。它看起来像会话变量不是一个对象,但$current_user是,即使它具有空属性。有人能指引我朝着正确的方向吗?谢谢。

+2

注意的功能是'__construct()',而不是'__costruct()'或'_construct '...并且不要在会话中序列化对象,除非在没有自动载入的情况下加载类定义之前,由于某种原因而需要调用'session_start()'。它将在保存在会话中的序列号中,无需执行两次。 – Wrikken 2013-03-13 21:42:50

+0

没有查看所有代码,但是在序列化/反序列化之前,您是否尝试过查看'$ _SESSION ['current_user']'或'$ current_user'的值? – Dave 2013-03-13 21:45:11

+0

您的构造函数被设计为解析传递给对象实例的多个变量,同时将单个(数组?)值传递给您的类。我认为有什么不对。 – 2013-03-13 21:47:34

回答

2

你的构造看起来错了,你想要做的事,我想你应该这样来做:

/** 
* Constructor set each public values with the ones passed from the passed array 
* @param array $userDatas Array containing the user datas 
*/ 
function __construct($userDatas) { 
    foreach($userDatas as $key => $value) { 
     if (property_exists($this, $key)) { 
      $this->$key = $value; 
     } 

    } 
} 
+0

这个技巧。首先,我担心错误构造是问题,但是在我的代码中它拼写正确,“n”肯定是我急于尝试将它格式化并张贴并离开办公室而受到伤害的原因,而“gettin”是好。 – Tony 2013-03-13 23:33:19