In standard non-MVC PHP I can use error_get_last() to find my last PHP error easily.
However in Zend, error_get_last() does not find any errors that occurred in the view. I am using it in my layout, and it finds errors in the layout, but I need it to find errors anywhere.
Is there some Zend class I should be using to get the last PHP error with global visibility?
If the Zend framework is object oriented, it is likely using exceptions, not errors. These are not the same, so error_get_last will work just not with exceptions. Exceptions only live to the catch, or force a termination if not caught.
Best you can do is retrieve the last assigned variable to the catch as PHP will not unset the assigned variable beyond the catch block.
I am able to see all exceptions raised from try/catch, so no problem there.
I want to get the last PHP error in my view, e.g. Notice: Using non-existent variable etc.
get_last_error() works if I put it in the view that has the error, but doesn't see the error if I use get_last_error() in the layout or in a view helper.
I think it is a visibility/scope issue, and wonder if there is a specific Zend class I should be calling to get the last simple syntax or other general error.
However, the below works (in layout.phtml) as expected.
But if I remove the first line, error_get_last() does not find a similar deliberate error in my view.
Code:
echo $this_var_does_not_exist; // THIS WILL CAUSE AN ERROR
echo $this->layout()->content; // CONTENT FROM VIEW
$this->arr_error_get_last = error_get_last();
print_r($this->arr_error_get_last);
In the code that I submitted, that is true, but there is also an error in the view that uses this layout - and I would expect that error to be picked up by error_get_last() once I remove the error that you see here.
The issue is that the error inmy view is not being found by error_get_last() in my layout.
Could it be that the controller runs the layout before it runs the view, so the error in the view has not yet occurred when the layout is run?
Its certainly possible; I know nothing of the zend framework. Objects can be tricky with this; there is nothing in place that says I cannot generate the output via referencing and populated it after the fact (kind of like the way prepared statements work when you bind results and variables to the statement). I can see this especially being the case should the output be something that is used repeatedly for different output.
I can't test this where I am. I wouldn't expect an inclusion to have any impact on the use of error_get_last as that would be linked prior to runtime. Another thing you *could* try is to use the $php_errormsg (with track_errors enabled in ini) as a reference. I don't know if it will work given the nature of the special $php_errormsg variable, but I know for sure it won't work for error_get_last. So you can't get a trace, only a message.
That would be a simple test as:
Thanks for all your help Fou-Lu .
I think it must be a Zend MVC thing.
I will get back to it at some stage soon when I have a bit more time.
Thank again.