php 注意:尝试获取非对象的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8865701/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:39:51  来源:igfitidea点击:

Notice: Trying to get property of non-object

php

提问by talhaMalik22

i am facing this problem here. i want to show item's name by getting its item id from another table. following is the code
i am having problem when i try to show name using ".$obj[0]->name." in the first line of the for loop.

我在这里面临这个问题。我想通过从另一个表中获取项目 ID 来显示项目的名称。以下是
当我尝试使用“.$obj[0]->name”显示名称时遇到问题的代码。在 for 循环的第一行。

$objClass = array(); 
$objClass1 = array();
$obj= array(); 
$object = new product();
$objLogic = new customerLogic();
$objLogic1 = new customerLogic(); 
$objL= new productLogic();
$objClass[0]= new stdClass;
$objClass1[0]= new stdClass;
$obj[0]= new stdClass;
$objClass[0]->custId = $_GET['id'];
$objClass1[0]->custId = $_GET['id'];

$objClass = $objLogic->getSaleRecord_customer($objClass[0]);
$objClass1 = $objLogic1->getName_customer($objClass1[0]);
$object->itemId = $objClass[0]->itemId;
$obj =$objL->getName_product($object->itemId); 
// echo $objClass1[0]->firstName;
$i=1;
foreach($objClass as $customer ) {
    echo "<tr><td class=\"inner_text\">$customer->reciept</td><td align=\"center\">".$obj[0]->name."</td>";
    echo "<td align=\"center\">".$objClass1[0]->firstName."&nbsp;".$objClass1[0]->lastName."</td><td align=\"center\">";
    echo "$customer->weight</td>
    <td align=\"center\">$customer->costPerKg</td>
    <td align=\"center\">$customer->cost</td>
    <td align=\"center\">$customer->payed</td>
    <td align=\"center\">$customer->remaining</td></tr>";
    $i++;
}
?>
</table>

采纳答案by Milad Naseri

Are you completely sure that $objL->getName_product($object->itemId);yields an object?

你完全确定这会$objL->getName_product($object->itemId);产生一个对象吗?

Could you verify that?

你能验证一下吗?

I think the return value of $objL->getName_product($object->itemId);is not what you think it is.

我认为的返回值$objL->getName_product($object->itemId);不是您认为的那样。

You can check it by print_r($objL->getName_product($object->itemId));which will give you a printout of the contents of that functions output.

您可以通过print_r($objL->getName_product($object->itemId));它来检查它,它将为您提供该函数输出内容的打印输出。

Or maybe you are unintentionally overriding the contents of $obj?

或者您可能无意中覆盖了$obj?

回答by Pekka

You are overwriting $obj:

您正在覆盖$obj

$obj =$objL->getName_product($object->itemId);