CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Zend and error_get_last() (http://www.codingforums.com/showthread.php?t=283466)

john_w3 12-03-2012 03:08 PM

Zend and error_get_last()
 
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?

Thanks.

Fou-Lu 12-03-2012 03:39 PM

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.

john_w3 12-03-2012 04:01 PM

Zend get_last_error
 
Thanks for the reply Fou-Lu.

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.

Thanks in advance for any time-saving help.

Fou-Lu 12-03-2012 04:18 PM

That won't be a scope issue. That sounds like an order of execution issue. error_get_last should work independently of any scope:
PHP Code:

error_reporting(0);

function 
func()
{
    print 
$var;
}

func();
print_r(error_get_last()); 

will result in
Code:

Array
(
    [type] => 8
    [message] => Undefined variable: var
    [file] => /t.php
    [line] => 7
)

So the problem is that the error is triggered sometime after the processing has occurred where you have put it.

john_w3 12-03-2012 04:50 PM

Good point, could be the issue.

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);

Thanks for your help, much appreciated.

Fou-Lu 12-03-2012 05:33 PM

I'm not seeing the issue here.
If you remove the line that triggers the error, then error_get_last will have no error to report.

john_w3 12-03-2012 05:47 PM

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?

Fou-Lu 12-03-2012 05:56 PM

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:
PHP Code:

$err = &$php_errormsg;
$a 0;
printf('Last error: %s'$err); 

if that shows division by 0 error, than the $php_errormsg can be referenced (normally a variable can be, but this one is a bit on the special side).

john_w3 12-04-2012 02:48 PM

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.


All times are GMT +1. The time now is 08:09 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.