doujiong2533 2014-10-23 11:56
浏览 108
已采纳

使用debug_print_backtrace的PHP异常错误

On my site, users can create pages, and assign collaborators. In my Page object, I store the collaborators as User objects in an array.

I also have a method called collaborators which accepts numerous different parameter types in order to manipulate the collaborators. For instance, if null is passed, it returns the existing collaborators. If an array is passed, it adds each user in the array to the array of collaborators.

This is my collaborators method:

public function collaborators($user = null)
{
    if(is_null($this->collaborators) || $user === true){

        if($this->resource_id() === false){
            $this->collaborators = array();
        } else {
            $result = $this->db->query("
                /** This retrieves collaborator user IDs **/
            ", array(
                $this->resource_id()
            ));

            $this->collaborators = array();

            foreach($result->result() as $user){
                $this->collaborators[] = new User($user->global_user_id);
            }
        }
    }

    if(is_null($user)){
        return $this->collaborators;
    } else if(is_string($user)){
        if(User::is_valid_id($user)){
            $this->collaborators[] = new User($user);
        }
    } else if(is_array($user)){
        foreach($user as $u){
            if($u instanceof User){
                $this->collaborators[] = $u;
            } else if(User::is_valid_id($u)){
                $this->collaborators[] = new User($u);
            }
        }
    } else if($user instanceof User){
        $this->collaborators[] = $user;
    } else {

        var_dump($user);
        throw new InvalidParametersException();
    }

    return $this;
}

In my view, I use a foreach loop to loop through collaborators to output them:

foreach($page->collaborators() as $user){ /** do stuff **/ }

As you can see in the collaborator method, if the object collaborator property is null, it retrieves data from the database. All subsequent calls to collaborators() retrieves the array from the collaborators property.

I have 2 problems:

I'm getting the following backtrace in my view:

object(stdClass)#529 (1) { ["global_user_id"]=> string(36) "8c9752fe-40d0-4259-bfe7-a5dcea7cbdfa" } 

Fatal error: Uncaught exception 'InvalidParametersException' with message 'There was an error while trying to process your request. Please try again later.' in application/libraries/objects/MinisiteResource.php:423 

Stack trace: 
#0 application/views/department_site/page.php(153): MinisiteResource->collaborators()
#1 system/core/Loader.php(833): include('/...') 
#2 system/core/Loader.php(419): CI_Loader->_ci_load(Array) 
#3 application/libraries/Pages.php(98): CI_Loader->view('department_site...', Array) 
#4 application/controllers/department/minisite.php(574): Pages->build_page('department_site...', Array) 
#5 [internal function]: Minisite->pages('8', '40') 
#6 system/core/CodeIgniter.php(359): call_user_func_array(Array, Array) 
#7 /v in application/libraries/objects/MinisiteResource.php on line 423

This backtrace does't make sense. You can see #0 is calling MinisiteResource->collaborators(). This defaults to a null parameter, so should return an array from the collaborators() method. However, for some reason its actually being passed an stdClass object from somewhere.

Now I want to know where this stdClass object is coming from, and this is my second problem

I add an extra line of code, to catch stdClass objects:

} else if($user instanceof stdClass) { echo 'Testing'; debug_print_backtrace(); exit; }

However, when I run this code, my page goes blank. I know for a fact that this segment of code IS running, but debug_print_backtrace() is failing. If I remove the debug_print_backtrace() call, I get 'Testing' outputted.

Obviously, if I can find out where the stdClass is being injected into the method, I can work out all my problems. But the main problem is debug_print_backtrace() is outputting nothing, and I can't rely on the Exception trace because for some reason it's not reliable.

Why would debug_print_backtrace() not display anything? There's definately enough method calls up to this point which should output at least something

If you need any further information, please let me know.

  • 写回答

1条回答 默认 最新

  • dongtao9887 2014-10-23 12:02
    关注

    Your problem is here:

    foreach($result->result() as $user){
       $this->collaborators[] = new User($user->global_user_id);
    }
    

    After foreach is done $user still hangs in memory, and it is a proper stdClass. Just rename it to something else and you're fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)