2013-05-17 44 views
0

读取行为通过的mysqli从MySQL数据库对象 :: fetch_object(所有PHP5),他们看起来是这样的的var_dumpPHP的stdClass的不是对象“

class stdClass#5 (5) { 
    public $id => 
    string(2) "23" 
    public $started => 
    NULL 
    public $finished => 
    NULL 
    public $mode => 
    string(3) "XML" 
    public $mail => 
    string(0) "" 
} 

现在就这样做:

public function __construct($export) { 
     var_dump($export); 
     if (!($export instanceof stdClass)) { 
//throw new exception ... 
} 

或本

public function __construct(stdClass $export) { 
     var_dump($export); 
//... 

,甚至与is_object($export) - 失败

其实我得到一个异常:

Fatal error: Uncaught exception 'Exception' 
with message '$export is not an object' 

Argument 1 passed to ConverterXML::__construct() 
must be an instance of stdClass, none given 
  1. 为什么
    • 甚至更​​好 -
  2. 如何我去检查的天气$出口是从mysqli的fetch_object的anonymouse类?
+2

嗯... http://3v4l.org/MJtWp?你确定你确实传递了一些东西/正确的东西? “***无***给”似乎是另有说法。 – deceze

+1

请参阅[fetch-object](http://php.net/manual/en/mysqli-result.fetch-object.php)手册,您可以设置class_name,但如果未指定,则返回'stdClass'对象。 –

+0

var_dump显示$ export包含更多的'None'。它实际上包含一个stdClass。 – DanFromGermany

回答

0

应该没有问题,如果你正在做正确是这样的:

$result = $mysqli->query($query); 
while ($obj = $result->fetch_object()) { 
    var_dump($obj); 
    var_dump($obj instanceof stdClass); 
    var_dump(is_object($obj)); 
} 

,或者,如果你想不同对象的实例比stdClass的:

class Employee { 
    public function __construct($id) { 
     $this->id = $id; 
    } 
} 
$i = 0; 
while ($obj = $result->fetch_object("Employee", array($i))) { 
    var_dump($obj); 
    $i++; 
}