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

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建