1

In the building process of an array I'm trying to call a function, checkIfNodeExists(). PHP executes this function and gives me the expected result but he still gives me a "Notice", and I don't want any kind of errors in my code.

function checkIfNodeExists($input) {
   if (isset($input)) {
      return (string) $input;
   } else {
      return 'null';
   }
}

$array['sections'][] = array (
   'ident'       =>  $section->attributes()->ident,
   'title'       =>  $section->attributes()->title,
   'objective'   =>  checkIfNodeExists($section->objectives->material->mattext)
);

Notice: Trying to get property of non-object in /var/www/OLAT_Connection/QTI-XML-Parser.php on line xx

Now, if I check if "objective" exists OUTSIDE the array, PHP doesn't give me a notice. But this results in more lines of code because I have to work with an extra variabele and more IF-structures and so on...

Is there any other possibility without adding too much lines of extra code?

2
  • Make sure that $section->objectives->material->mattext is properly initiated and defined. Please note that more code is not necessarily worse! Commented Mar 26, 2012 at 9:02
  • maybe you should check with isset before you call checkIfNodeExists Commented Mar 26, 2012 at 9:11

4 Answers 4

2

The problem is that when you call checkIfNodeExists you send it a value, and by sending it the value you also execute the value. So isset() will work on the result of the expression $section->objectives->material->mattext and not the expression itself.

This would work:

$array['sections'][] = array (
   'ident'       =>  $section->attributes()->ident,
   'title'       =>  $section->attributes()->title,
   'objective'   =>  isset($section->objectives->material->mattext) ? (string)$section->objectives->material->mattext : 'null'
);
Sign up to request clarification or add additional context in comments.

Comments

1

It seems that $section->objectives->material->mattext is not properly defined. I would start looking there to see if you have errors in the initialisation of the object.

It would be better if you posted more code up so we can see what exactly is going on in this script.

The solution may require more code (albeit unlikely in this case), this is by no means a bad thing. Less code is not necessarily better or more efficient! Obviously it goes without saying that fewer lines of code will (most of the time) execute faster, but this does not make it more secure or efficient

UPDATE

You may be able to simply do this instead of calling another function:

'objective' => isset($section->objectives->material->mattext) ? (string)$section->objectives->material->mattext : null

I have not tested this and cannot remember whether you can place conditional statements inline, so cannot be sure whether it will work but if it does then this will be more efficient, and it is less code!

2 Comments

Well Halleluja, the conditional works. However I understand that more code isn't necessarily bad. Thanks!
Glad I could help, enjoy! :-)
0

if you add "@" when you call the function the error aren't show

    function checkIfNodeExists($input) {
   if (isset($input)) {
      return (string) $input;
   } else {
      return 'null';
   }
}

$array['sections'][] = array (
   'ident'       =>  $section->attributes()->ident,
   'title'       =>  $section->attributes()->title,
   'objective'   =>  @checkIfNodeExists($section->objectives->material->mattext)
);

1 Comment

Yes, but this just suppresses the error. The function is not doing what he wants so this does not sort it!
0

Define $section->objectives->material->mattext

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.