简体   繁体   English

PHP 在尝试获取非对象的属性时抛出错误

[英]PHP throwing an error trying to get property of a non-object

This is an old chestnut but I cannot see the error in my code.这是一个老栗子,但我看不到我的代码中的错误。 I'm in Windows 10, VSCode and using the Codeigniter 3 framework.我在 Windows 10、VSCode 中并使用 Codeigniter 3 框架。 I am trying to delete a record but also add an alert saying that the record has been successfully deleted.我正在尝试删除一条记录,但还添加了一条警报,说明该记录已成功删除。 So first I use the id and get the record, save it as session data, delete the record and then put up the alert using the saved data like this 'You have successfully deleted the database entry for John Smith'.因此,首先我使用 id 并获取记录,将其保存为会话数据,删除记录,然后使用保存的数据发出警报,例如“您已成功删除 John Smith 的数据库条目”。 Here is my controller method:这是我的控制器方法:

function member_delete($id) {
  $member = $this->get_where($id);
  $this->session->set_userdata('firstname',$member->firstname);
  $this->session->set_userdata('lastname',$member->lastname);
  if($this->_delete($id)){
    $firstname = $this->session->userdata('firstname');
    $lastname = $this->session->userdata('lastname');
    $this->session->set_flashdata('success', 'You have successfully deleted the database entry for '.$firstname.' '.$lastname);
    $this->members();
  } else {
    $this->session->set_flashdata('failure','There has been an error and the record has not been deleted');
    $this->members();
  }
}

This works fine and the alert is successfully posted but then when I then refresh the page PHP throws an (Severity:notice) error 'Trying to get the property of a non-object'.这工作正常,警报已成功发布,但是当我刷新页面时,PHP 抛出一个 (Severity:notice) 错误“试图获取非对象的属性”。 The error line is the second line of the method ie $method->firstname.错误行是方法的第二行,即 $method->firstname。

I have run var_dump on the variable $member and it is definitely an object, being returned with result->row().我在变量 $member 上运行了 var_dump,它绝对是一个对象,用 result->row() 返回。

Can anyone tell me what is going wrong?谁能告诉我出了什么问题?

You're fetching $member - but then you try to access properties without knowing $member was successfully filled in the previous line您正在获取 $member - 但随后您尝试访问属性而不知道 $member 已在前一行中成功填写

So I'd start with :所以我会开始:

$member = $this->get_where($id);
if ( is_a($member, "someClass") )
{
  $this->session->set_userdata('firstname',$member->firstname);
  $this->session->set_userdata('lastname',$member->lastname);
} else
   // process failure

You said the var_dump() returns an object - but does it return the object you're expecting ?您说 var_dump() 返回一个对象 - 但它是否返回您期望的对象? Do all expected properties exist ?所有预期的属性都存在吗?

Secondly, who or what is calling this function ?其次,谁或什么在调用这个函数? If you do a refresh of the page, obviously member_delete() is called with a value of $id which shouldn't be there anymore (as you deleted it the last cycle).如果您刷新页面,显然 member_delete() 将使用 $id 值调用,该值不应再存在(因为您在最后一个循环中删除了它)。 So either $id is persistent for some reason, or something is invoking it with obsolete data因此,要么 $id 出于某种原因是持久的,要么使用过时的数据调用它

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

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