Php 致命错误:对非对象调用成员函数isupload()
我面临一条错误消息:Php 致命错误:对非对象调用成员函数isupload(),php,zend-framework,fatal-error,Php,Zend Framework,Fatal Error,我面临一条错误消息: 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 据我所知,弹出此错误消息是因为我正在调用对象中不存在的方法。但我确信ISupload确实存在 IsUpload函数在类Zend_Form_Eleme
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
据我所知,弹出此错误消息是因为我正在调用对象中不存在的方法。但我确信ISupload确实存在
IsUpload函数在类Zend_Form_Element_文件中定义。为了检查$xls是否是Zend_Form_Element_文件的实例,我调试了$xls变量
Zend_Debug::dump($xls); //OUTPUT: object(Zend_Form_Element_File)#141 (29) {
exit;
echo gettype($xls); //the output was object
exit;
第30行是这样的:
if ( $xls->isUploaded() ) {
我做的第一件事是检查表达式值
Zend_Debug::dump($xls->isUploaded()); //the output was: bool(true)
exit;
然后我检查了$xls变量的类型
Zend_Debug::dump($xls); //OUTPUT: object(Zend_Form_Element_File)#141 (29) {
exit;
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
}
}
}
}
将此项添加到您的条件中以避免致命错误:
我很确定handleImport方法被多次调用,可能是在一个循环中,可能是$name参数的不同值。您回显变量并对其进行调试,如果在第一次运行时为$name提供的值是正确的,那么这将非常有效。但是,由于您终止了脚本,因此您不会获得任何有关后续调用的调试信息 在调用对象之前,请确保该对象具有该方法。您可以调用方法_exists或instanceof进行确定 代码:
在非对象上表示$xls不是任何类的对象。它可能是可变的……但在echo gettype$xlss之后,它显示了object,为什么是这样?您能粘贴引发此错误的代码段吗?这很有效,但我不明白为什么。@Julian Well显然您正在处理的代码正在运行多次。它第一次起作用是因为$xlss实际上是一个您所检查的对象。上次它不起作用是因为$xlss不是一个对象,但你没有检查它。Halayem Anis-这是一个很好的解决方案,但值得一提的是,OP需要检查为什么$xlss不是一个对象,并查看是否有其他东西需要基于此进行修复。解释+1。