php - 在 null 上调用成员函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31800708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php - call to a member function on null
提问by Hoffa
Apologies if this is realbasic, but when PHP gets into functions I'm over my head.
抱歉,如果这是真正的基本知识,但是当 PHP 进入功能时,我不知所措。
I have a plug-in for a forum which loads a flash cookie as a method to detect duplicate accounts.
我有一个论坛插件,它加载 flash cookie 作为检测重复帐户的方法。
The script is failing on about 0.1% of page views, causing a WSOD. The error points to this line:
该脚本在大约 0.1% 的页面浏览量上失败,导致 WSOD。错误指向这一行:
return $this->registry->output->getTemplate( 'dml' )->duplicatesLoadMovie( $host, $path, $id, $md5 );
and the error is:
错误是:
Fatal error: Call to a member function duplicatesLoadMovie() on null in /var/www/.../web/forums/hooks/duplicatesLoadMovie.php on line 75
致命错误:在第 75 行的 /var/www/.../web/forums/hooks/duplicatesLoadMovie.php 中,在 null 上调用成员函数 duplicatesLoadMovie()
To me, this reads as one of $host
, $path
, $id
, or $md5
is returning null
, but as this is not my script (and the coder is, of course, unresponsive), I would like to fix this in the simplest way possible (other than removing it, which is the fallback position).
对我来说,这读作$host
, $path
, $id
, 或$md5
is returns 之一null
,但由于这不是我的脚本(当然,编码器没有响应),我想以最简单的方式解决这个问题(除了删除它) ,这是后备位置)。
Can I simply do something to the effect of $id = 0 if id.null
? (sorry for the Rubyspeak) for each of the $variables
?
我可以简单地做一些事情$id = 0 if id.null
吗?(对不起 Rubyspeak)对于每个$variables
?
Full source of the file:
文件的完整来源:
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 );
}
采纳答案by Source Matters
I'd suspect that this getTemplate( 'dml' )
is not returning a value. If you do a var_dump(getTemplate( 'dml' ))
, does it show anything?
我怀疑这getTemplate( 'dml' )
没有返回值。如果你做 a var_dump(getTemplate( 'dml' ))
,它会显示什么吗?
You might check for "null" before doing that line of code, and in your "else" statement, output an error message, or some other action suitable for that error.
您可能会在执行该行代码之前检查“null”,并在“else”语句中输出错误消息或其他适合该错误的操作。