문제

Fetching rows as objects from a MySQL database via mysqli::fetch_object (all PHP5), they look like this on 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) ""
}

Now on doing this:

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

or this

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

or even with is_object($export) - this fails

I actually get an Exception:

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

or

Argument 1 passed to ConverterXML::__construct()
must be an instance of stdClass, none given
  1. why
    • or even better -
  2. How do I get to check weather $export is an anonymouse class from mysqli fetch_object?
도움이 되었습니까?

해결책

there should be no problems, if you are doing it correctly like this:

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

or, if you want instance of different object than stdClass:

class Employee {
    public function __construct( $id ) {
        $this->id = $id;
    }
}
$i = 0;
while ( $obj = $result->fetch_object( "Employee", array( $i ) ) ) {
    var_dump( $obj );
    $i++;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top