tags:

views:

52

answers:

3

On my localhost, if I do echo $a->b where $a is not an object, it shows nothing. But on another server, it gives me an error of "Trying to get property of non-object". How to ignore this error? I just want a quick and easy way to make the code running, and I know what I am doing.

A: 

try use something like

error_reporting(E_ALL & ~E_NOTICE);

in first line of your code.

heximal
Better delete this "answer"
Col. Shrapnel
@Col - I know what I am doing. The site just run into a tricky bug which cannot be fixed with simply adding something like `if is_object($a)`. So I need a quick fix temporarily to make the site live. I'll fix it later.
Ethan
This one is accepted? So instead of fixing the error, you will simply ignore it? Way to go. -1 since there isn't any "stupid" option to select.
David Kuridža
@Ethan So you've got production site that displays all errors... Indeed, you know what you are doing.
dev-null-dweller
+1  A: 

If you just want to not display this notice you have the option of turning off the notices by including this snippet at the beginning of your script:

error_reporting(0);
// or error_reporting(E_ALL & ~E_NOTICE); to show errors but not notices
ini_set("display_errors", 0); 

This could also be setup globally in the server settings.

Alternatively you can ignore the notice on a specific line by using the @ symbol, like so:

echo @$a->b;

That would suppress the notice for that single statement.

The third, most 'proper', most time consuming, and most workload expensive option would be to add checks around each such code segment, to ensure it has been set prior to trying to read it, Jacob's suggestion.

dsclementsen
A: 

check the php.ini, in the section of errors.

alfdev