通过 mysqli :: fetch_object (所有 PHP5 )从MySQL数据库中获取行作为对象,它们在上看起来像这样的var_dump :
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
答案 0 :(得分: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++;
}