繁体   English   中英

PHP和Smarty错误:试图获取非对象的属性

[英]PHP and Smarty Error: Trying to get property of non-object

我是这个网站的新手并且学习PHP。 我正在使用Darie和Bucica的文本Beginning PHP5和MySQL电子商务从新手到专业来创建一个电子商务网站。 我相信到目前为止我遇到的一些错误是由于更新的数据库(MDB2)。 除了这个,我已经能够克服每一个错误。 该代码应该使用Smarty从我的数据库中提取部门列表。

我在最后一行收到错误“试图获取非对象的属性”。 我觉得它与is_array()函数有关。

<?php $_smarty_tpl->tpl_vars["load_departments_list"] = new Smarty_variable("departments_list", null, null);?>
    <table border="0" cellpadding="0" cellspacing="1" width="200">
     <tr>
      <td class="DepartmentListHead"> Choose a Sport </td>
     </tr>
     <tr>
      <td class="DepartmentListContent">
       <?php unset($_smarty_tpl->tpl_vars['smarty']->value['section']['i']);
    $_smarty_tpl->tpl_vars['smarty']->value['section']['i']['name'] = 'i';
    $_smarty_tpl->tpl_vars['smarty']->value['section']['i']['loop'] = is_array($_loop=$_smarty_tpl->getVariable('departments_list')->value->mDepartments) ? count($_loop) : max(0, (int)$_loop); unset($_loop);

如果您还需要其他任何帮助,请告诉我们! 请尽可能描述,并尽可能使用我的代码显示解决方案。 谢谢你的帮助! -DREW

您正在使用in_array函数中的$_smarty_tpl->getVariable('departments_list')->value->mDepartments 确保将departments_list分配给smarty对象。

或者在此之前添加一张支票

$departments_list = $_smarty_tpl->getVariable('departments_list');
if (is_object($departments_list) && is_object($departments_list->value)
         &&  $departments_list->value->mDepartments) {
    $_smarty_tpl->tpl_vars['smarty']->value['section']['i']['loop'] = is_array($_loop=$_smarty_tpl->getVariable('departments_list')->value->mDepartments) ? count($_loop) : max(0, (int)$_loop); unset($_loop);
}

尝试使用var_dump()检查每个变量的类型:

var_dump($_smarty_tpl->getVariable('departments_list'), $_smarty_tpl->getVariable('departments_list'))->value,
$_smarty_tpl->getVariable('departments_list'))->value->mDepartments);

这将告诉你什么类型的值。 问题实际上并不在于is_array函数,但实际上,在$_smarty_tpl->getVariable('departments_list'))->value->mDepartments情况下,你试图在两种情况下访问对象属性,返回值为getVariable()方法和getVariable()->value ,所以这两个中的一个给你带来了麻烦。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM