【问题标题】:Call to undefined method stdClass调用未定义的方法 stdClass
【发布时间】:2015-03-27 12:58:51
【问题描述】:

我偶尔会收到 PHP 致命错误:在第 25 行的 agent.php 中调用未定义的方法 stdClass::transition()(我在代码中标记了第 25 行)。这段代码经常被调用,所以很难理解它为什么会发生。

这里是agent.php的sn-p调用

   function agent_exam_complete($exam){
    $ce = $exam->educational();
    $ce->exam_id = $exam->exam_id;
    $ce->exam_grade = $exam->score;
    $ce->exams_remaining -= 1;
    $ce->exam_received_date = sql_now();
    if($exam->status()=='passed'){
        $ce->transition('passed');
    }elseif($ce->exams_remaining <= 0){
        $ce->transition('failed');
    }
    $ce->save();

    if($ce->is_certification_completed($ce->certification_id, $ce->client_no)){
        agent_certification_complete($ce->certification_id, $ce->client_no);
    }
}


function agent_certification_complete($certification_id, $client_no){
    $ce = ClientPurchase::find('first', array('conditions' => "certification_id = '$certification_id' and is_certification = 1 and client_no='$client_no'"));
    $ce->certification_date = date('Y-m-d');
    $ce->transition('passed'); **//Line 25**
    $ce->save();
}

transition() 在另一个文件中定义并且经常被调用。我已经包含了一些它的代码只是为了味道。

function transition($event_tag){
    $old_status = $this->status;
    $next_status = $this->next_status_for_transition($event_tag);
    if($next_status==''){ 
    return; }
    $this->status = $next_status;

我的问题是,为什么我只是定期而不是一直收到此错误?我可以做些什么来为我的客户消除错误和随后的空白屏幕?我只注意到使用 Firefox 或 Chrome 的用户会发生这种情况。

提前致谢, 吉姆

【问题讨论】:

  • 很奇怪。由于 PHP 是服务器端的,它不应该影响特定的浏览器。
  • 我同意。不知道每个浏览器是否设置了可能导致问题的不同超时。抓住稻草。
  • PHP 的任何超时也将在 PHP 或 Apache 配置中设置在服务器端。日志中是否有任何错误?
  • 你能把ClientPurchase 类也包括进来吗?或者更好的是,该类中的 find 方法返回什么?
  • 就是我发的那个,又来了:PHP Fatal error: Call to undefined method stdClass::transition() in agent.php on line 25

标签: php


【解决方案1】:

包含该函数的对象$ce 正在生成多次。我想这是transition 是为任何被调用的对象定制的。

为什么不为可重用功能创建另一个对象?考虑扩展该函数,使其与所有使用它的对象兼容。

$my = new functionClass;

class functionClass
{
    function transition()
    {
        $old_status = $this->status;
        $next_status = $this->next_status_for_transition($event_tag);
        if($next_status==''){ 
        return; }
        $this->status = $next_status;
    }
}

$my->transition( 'passed' );

这样的事情会减少不可预测性,我相信可以解决您的问题。

【讨论】:

    【解决方案2】:

    试试这个小sn-p代码看看发生了什么:

    $ce = false;
    $ce->certification_date = date('Y-m-d');
    var_dump($ce);
    

    在这种情况下,当您尝试设置属性 (certification_date) 时,$ce 会被强制转换为 stdClass 的对象。

    现在你的代码:

    function agent_certification_complete($certification_id, $client_no){
        $ce = ClientPurchase::find('first', array('conditions' => "certification_id = '$certification_id' and is_certification = 1 and client_no='$client_no'"));
        //$ce is probably false or null
        //it gets cast to a stdClass object
        $ce->certification_date = date('Y-m-d');
        //stdClass does not have a transition method; ERROR
        $ce->transition('passed'); **//Line 25**
        $ce->save();
    }
    

    因此,在您的代码中,如果 find() 返回 null 或 false,或者可能是其他一些选择值,$ce 将被强制转换为下一行的 stdClass 对象。然后那个 stdClass 对象没有 transition() 方法,所以你得到一个错误。 要解决此问题,请调整您的 find 方法或检查其返回值并进行相应处理。

    至于它只发生在某些浏览器中,我认为这是一个错误的结论。如果find() 正在调用查询,它可能仅在特定时间发生,具体取决于该查询的结果。

    【讨论】:

      猜你喜欢
      • 2017-04-24
      • 2019-11-20
      • 1970-01-01
      • 1970-01-01
      • 2012-04-02
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 2020-06-02
      相关资源
      最近更新 更多