简体   繁体   English

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

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

I am new to both this site and learning PHP. 我是这个网站的新手并且学习PHP。 I am using the text Beginning PHP5 and MySQL E-Commerce From Novice to Professional by Darie and Bucica to create an e-commerce website. 我正在使用Darie和Bucica的文本Beginning PHP5和MySQL电子商务从新手到专业来创建一个电子商务网站。 I believe some of the errors I have encountered so far were due to the updated database (MDB2). 我相信到目前为止我遇到的一些错误是由于更新的数据库(MDB2)。 I have been able to get past every error except this one. 除了这个,我已经能够克服每一个错误。 The code is supposed to pull the list of departments from my database using Smarty. 该代码应该使用Smarty从我的数据库中提取部门列表。

I get the error "Trying to get property of non-object" on the last line. 我在最后一行收到错误“试图获取非对象的属性”。 I have a feeling it has to do with the is_array() function. 我觉得它与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);

If there is anything else you need to help answer please let me know! 如果您还需要其他任何帮助,请告诉我们! Please be as descriptive as possible and show the solution using my code if possible. 请尽可能描述,并尽可能使用我的代码显示解决方案。 Thanks for your help! 谢谢你的帮助! -Drew -DREW

Your are using $_smarty_tpl->getVariable('departments_list')->value->mDepartments inside your in_array function. 您正在使用in_array函数中的$_smarty_tpl->getVariable('departments_list')->value->mDepartments Make sure that you assigned departments_list to a smarty object. 确保将departments_list分配给smarty对象。

OR add a check before that 或者在此之前添加一张支票

$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);
}

Try checking each variable for type using var_dump() : 尝试使用var_dump()检查每个变量的类型:

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

That will tell you what type values are. 这将告诉你什么类型的值。 The problem doesn't really lie in is_array function, but in the fact, that with $_smarty_tpl->getVariable('departments_list'))->value->mDepartments you are trying to access object property in two cases, on returned value of getVariable() method, and getVariable()->value , so one of those two is doing you trouble. 问题实际上并不在于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