“试图获取非对象的属性";在PHP中 [英] "Trying to get properties on a non-object" in PHP
问题描述
我是WordPress主题开发人员,并且在我正在处理的主题上,我已打开调试模式,并在该主题的选项页面的选择下拉框中找到此错误.
I'm a WordPress theme developer and on a theme I'm working on, I've turned on debug mode and get this error inside a select drop-down box for the theme's options page.
该错误表明:试图获取非对象的属性."这是令人反感的代码:
The error says this: "Trying to get properties on a non-object." Here is the offending code:
<select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
<?php foreach ($value['options'] as $option) { ?>
<option value="<?php echo $option->term_id; ?>" <?php if ( $settings[$id] == $option->term_id) { echo 'selected="selected"'; }?>>
<?php echo $option->name; ?>
</option>
<?php }?> </select>
我不知道自己在做什么错.专门导致该错误的行是这样的:
I can't figure out what I'm doing wrong. The line that's specifically causing the error is this:
<option value="<?php echo $option->term_id; ?>" <?php if ( $settings[$id] == $option->term_id) { echo 'selected="selected"'; }?>>
感谢您的任何建议! :)
Thanks for any advice! :)
推荐答案
您要这样做几次:
$option->someproperty
在至少一种情况下,$option
不是对象.然后,对非对象执行->
时,会出现该错误.
In at least one of the cases $option
is not an object. When you then do ->
on a non-object, you get that error.
首先,确认$option
实际上是一个对象,如果不是,请更正用法.
First, verify that $option
is actually an object and correct the usage if it is not.
如果$option
是一个数组,则@matthewpavkov是正确的,您应该执行$option['someproperty']
而不是$option->someproperty
.
If $option
is an array then @matthewpavkov is correct, you should do $option['someproperty']
instead of $option->someproperty
.
如果$option
通常是一个对象,则get_categories()
有时会在失败的情况下返回null
或false
.在访问$option
之前进行检查.
If $option
is usually an object, perhaps get_categories()
is sometimes returning null
or false
in a failure condition. Check for that before accessing $option
.
像这样:
foreach ($value['options'] as $option)
{
if ($option)
{
// do whatever;
}
}
这篇关于“试图获取非对象的属性";在PHP中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!