Click to See Complete Forum and Search --> : PHP Call to a member function on a non-object - Help!


jsdd3
June 23rd, 2009, 12:31 AM
Hi There,

I have come across a very frustrating issue, which I see is quite a popular one. The following bit of code saves information to an XML file, but since upgrading to PHP 5.2.0 I get the following error:

Call to a member function on a non-object

It is very difficult for us (virtually impossible) to downgrade our version of PHP both from an infrastructural point of view, and also we need to ensure we have the latest version.

I would really appreciate it if anyone could be of assistance in helping me change the code below. The two critical lines in each are:

$this->questions[$idx]->setQuestionText($questionField);
$this->questions[$question_index_id]->fields[$field_index_id]->setHint($hint);

Code snippet:

function getContdevEditorOutput($challenge_id=0,$tutorial_id=0) {
$this->challenge_id = $challenge_id;
$this->tutorial_id = $tutorial_id;
//error_reporting(E_ALL);
$form = (isset($_POST['formvars']) ? $_POST['formvars'] : array());

// check if form was submitted - all returned
if(isset($form['nodeEditForm']) && $form['elementToEdit']=="allfields") {
// save question text
$questionTextFields = $form['saveQText'];
echo "QUESTIONS:<br><br>";
print_r($this->questions);
foreach($questionTextFields as $idx=>$questionField)
{
$this->questions[$idx]->setQuestionText($questionField);

}


// save hints
$hints = $form['saveHint'];
foreach($hints as $idx=>$hint) {
$refArr = explode("_",$idx);
$question_index_id = $refArr[0];
$field_index_id = $refArr[1];

$this->questions[$question_index_id]->fields[$field_index_id]->setHint($hint);

}

// option texts

$options = $form['saveOptionText'];
foreach($options as $idx=>$option) {
$refArr = explode("_",$idx);
$question_index_id = $refArr[0];
$field_index_id = $refArr[1];
$option_index_id = $refArr[2];

if(!isset($questions[$question_index_id]))
$this->questions[$question_index_id]->fields[$field_index_id]->options[$option_index_id]->setLabel($option);

//print "<br/>IDX:".$idx;
//print_r($option);
}

Regards,
James Dey

PeejAvery
June 23rd, 2009, 06:21 AM
The error means that you are treating a variable as an object type when it is either not an object, or not even created at time of being referenced.

Double check questions[$idx], questions[$question_index_id], and fields[$field_index_id] to make sure that they are created, then use is_object() to make sure that they are objects at the time you reference them. 99% of the time this error is because the variable does not exist.