dongyongyu0789 2013-05-17 12:04
浏览 29
已采纳

PHP'stdClass不是对象'

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?
  • 写回答

1条回答 默认 最新

  • douduikai0562 2013-05-17 12:33
    关注

    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++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理