tags:

views:

30

answers:

2

I changed the option in the Joomla configuration section to show all errors (E_ALL) and my component is basically unusable.

As an example, in a view (editapp) I have a template, default.php, which contains things like:

    <input class="text_area" type="text" name="application_name" id="application_name" size="50" maxlength="250" value="<?php echo $item->application_name" ?> />

Now because of $item->application_name when I run this the first time (a new record), there will be notice errors everywhere Trying to get property of non-object

This is because the same template is used for saving and editing. I followed tutorials and they seemed to do it this way.

What is the go here with PHP development. Should I be checking these things? if(isset($item-)) {...}, what is the best practice for PHP development?

+1  A: 

It is absolutely worth getting rid of the errors and warnings in your code for a couple reasons:

  • Every error gets written to the error log. If they're inconsequential, you've polluted your error log with information that's in no way helpful.
  • Most warnings are indicative of bigger errors. The unset variable you're referencing can cause problems down the road in situations you may not have thought of.
  • Others who are using your template may not have the ability or knowledge to turn off errors, resulting in them not using your code.
Chris Henry
OK, should I be using `if( isset($item) ) {...}` on every occurrence or should I be using an completely different template for editing and saving?
jax
To add a 4th point to your list: errors, even suppressed errors, are relatively expensive. They have to pop up the stack to the error handler -- even suppressed errors; the error handler is responsible for deciding whether or not to respect the fact that an error has been suppressed.
Frank Farmer
@Frank - Totally agree, not to mention the expense of disk io for Apache to log. I was going to add that in, but it's kind of micro.
Chris Henry
It's really impressive how expensive logging is. We turned down the verbosity of apache's error log on our app servers recently, and load instantly dropped by something like 20-50%.
Frank Farmer
+2  A: 

I agree with the others - go for no errors. In fact, I suggest going beyond E_ALL and adding in E_STRICT, which is how I do all my development. There are good reasons for each of those notices, and you'll do well to be rid of them. The most notable reason is that the notices typically point out small bugs that can make it much harder to find and fix bigger bugs down the road. It's not too different from the idea of using assertions: they help you squish bugs as quickly as possible.

As to your specific example, rather than using isset() right before referencing it, I recommend initializing the object to a sane base state going into the page (perhaps using isset() at that point). For example, you might set the application_name value to an empty string. This will enable you to keep your output statement nice and clean but properly address the notice by making sure the variable is initialized before using it. (FWIW, in many languages, using unitialized variables causes an even more aggressive response in the form of a compile-time error.)

mr. w
jax