2014-10-31 27 views
-1

我面对错误消息的成员函数:致命错误:调用一个非对象isUploaded()

Fatal error: Call to a member function isUploaded() on a non-object in /www/htdocs/nether/http/123factuur/application/controllers/Helpers/ImportXls.php on line 30 

要我understanind此错误消息弹出,因为我打电话的方法这在对象中不存在。但我确信isUploaded()确实存在。

函数isUploaded在类Zend_Form_Element_File中定义。要检查$xls是否是Zend_Form_Element_File的实例,我调试了$xls变量。

Zend_Debug::dump($xls); //OUTPUT: object(Zend_Form_Element_File)#141 (29) { 
exit; 

第30行是这样的:

if ($xls->isUploaded()) { 

我做的第一件事是检查表达式的值。

Zend_Debug::dump($xls->isUploaded()); //the output was: bool(true) 
exit; 

然后我检查了$xls变量的类型。

echo gettype($xls); //the output was object 
exit; 

我不完全理解错误。也许,我没有解释错误信息,因为它应该被解释。无论如何,需要援助。

的代码片段: 在控制器:

public function importAction() { 
     $form = $this->getImportFrom(); 
     $this->view->form = $form; 
     $this->view->allowedHeaders = array(); 

     $this->importInvoices($form); 
     $this->importInvoiceArticles($form); 
     $this->importInvoiceServices($form); 
     foreach ($this->_lookupIssues as $issue) { 
      $this->_flashMessenger->addMessage($issue); 
     } 
    } 


public function importInvoiceArticles($form) { 
     $model = 'Invoice_article'; 
     $config = Zim_Properties::getConfig($model); 
     $Model = new Zim_Model($model, $config->model); 
     $headerMapping = array_flip(array_intersect_key($Model->getHeaders(true), array_flip($this->_allowedArticleImportHeaders))); 
     $this->getHelper('ImportXls')->handleImport($form, $headerMapping, $Model->getName(), $this->_modelName, null, null, array($this, 'saveImportedArticleData'), 'invoiceArticle'); 
    } 

在助手:

class F2g_Helper_ImportXls extends Zend_Controller_Action_Helper_Abstract { 

public function handleImport($form, $allowedHeaders, $tableName, $modelName, $onDuplicateImportCallback, $importMethod = null, $saveMethod = null, $name = 'xls') { 
     if ($this->getRequest()->isPost()) { 
      $xls = $form->getElement($name); 
      if ($xls->isUploaded()) { 
       //some code 
      } 
     } 
    } 
} 
+2

'在非object'意味着'$ xls'不是任何类的对象。它可能是可变的...... – Umair 2014-10-31 09:28:21

+0

但在echo echotype($ xlss)后显示'object',为什么会这样? – Julian 2014-10-31 09:29:56

+0

你能否粘贴引发这个错误的代码片段? – motanelu 2014-10-31 09:32:18

回答

1

添加到您的病情,以避免致命错误:

if (!empty($xls) && is_object($xls) && $xls->isUploaded()) { 
    // do your job with serenity :) 
} 
+0

那好,但我不明白为什么。 – Julian 2014-10-31 09:45:32

+1

@Julian很明显你正在编写的代码正在多次运行。第一次工作是因为'$ xlss'确实是一个对象(这是你检查的内容)。上一次它不起作用是因为'$ xlss'不是一个对象,但是你没有检查它,太糟糕了。 – 2014-10-31 09:57:33

+1

Halayem Anis - 这是一个很好的解决方案,但值得一提的是OP需要检查为什么'$ xlss'不是一个对象,并且看看是否有其他需要修复的东西。 – 2014-10-31 09:58:24

3

我很确定handleImport()方法被称为multipl e次,可能在循环中,可能为$ name参数使用不同的值。如果为第一次运行提供的$ name值是正确的 - 但是由于您终止了脚本 - 您不会获得有关后续调用的任何调试信息,您可以回显变量并死掉以进行调试。

在调用它之前请确保该对象具有该方法。您可以调用method_exists()或instanceof来做出决定。

代码:

if ($xls instanceof Zend_Form_Element_File) { 
    // object of correct type - continue (preferred version) 
} 

// or 

if (method_exists($xls, 'isUploaded')) { 
    // avoids the error, but does not guarantee that 
    // other methods of the Zend_Form_Element_File exist 
} 
+0

+1有关说明。 – Julian 2014-10-31 09:51:55

相关问题