php - 在null上调用成员函数

时间:2015-08-04 04:27:57

标签: php

如果这是真正的基本的道歉,但当PHP进入我的脑海中时,我会道歉。

我有一个论坛插件,可以加载Flash Cookie作为检测重复帐户的方法。

脚本在大约0.1%的页面查看时失败,导致WSOD。错误指向此行:

return $this->registry->output->getTemplate( 'dml' )->duplicatesLoadMovie( $host, $path, $id, $md5 );

,错误是:

  

致命错误:在第75行的/var/www/.../web/forums/hooks/duplicatesLoadMovie.php中调用null上的成员函数duplicatesLoadMovie()

对我来说,这是$host$path$id$md5之一正在返回null,但因为这不是我的脚本(编码器当然没有响应),我想以最简单的方式解决这个问题(除了删除它,这是后备位置)。

我可以简单地对$id = 0 if id.null的效果做些什么吗? (对不起,对于Rubyspeak)每个$variables

文件的完整来源:

class duplicatesLoadMovie
{ 
/**
 * Registry object shortcuts
 *
 * @var     $registry
 * @var     $settings
 * @var     $member
 * @var     $memberData
 **/
public $registry;
public $settings;
public $member;
public $memberData;

/**
 * Main function executed automatically by the controller
 *
 * @access  public
 * @return  @e void
 **/
public function __construct()
{
    $this->registry   = ipsRegistry::instance();
    $this->settings   =& $this->registry->fetchSettings();
    $this->member     = $this->registry->member();
    $this->memberData =& $this->registry->member()->fetchMemberData();
}

/**
 * Get the output
 *
 * @access  public
 * @return  @e string
 **/
public function getOutput()
{
    /* Grab host URL and installation path of the community */
    $host = $this->settings['board_url'];

    $parsed = parse_url( $host );
    $path = trim( $parsed['path'], '/' );
    if( $path == '' )
    {
        $path = '/';
    }

    $host = 'host=' . $host;
    $path = 'path=' . $path;

    /* Determine the appropriate identification method (member_id for members and session_id for guests) */
    if( $this->memberData['member_id'] )
    {
        $id = 'mid=' . $this->memberData['member_id'];
    }
    else
    {
        $id = 'sid=' . $this->member->session_id;
    }

    /* Grab the secure key for AJAX */
    $md5 = 'md5=' . $this->member->form_hash;

    /* Finally, call the template bit */
    return $this->registry->output->getTemplate( 'dml' )->duplicatesLoadMovie( $host, $path, $id, $md5 );
}

2 个答案:

答案 0 :(得分:2)

我怀疑这个getTemplate( 'dml' )没有返回值。如果你做var_dump(getTemplate( 'dml' )),它会显示什么吗?

您可以检查" null"在做那行代码之前,在你的" else"语句,输出错误消息或适合该错误的其他一些操作。

答案 1 :(得分:-1)

您的问题很久以前就已经解决了,但是我想很多人在寻找解决方案时都遇到了这个错误。 PHP 8.0引入了新的运算符nullsafe operator (?->)。这不是为了抑制错误(!),而是在允许变量为git clone的情况下使用,然后我们无需再次检查其值。

所以在OP的问题中,最后一行看起来像:

null

应该非常小心地使用它,因为这并不是每次出现此类错误的解决方案!