Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-03-2012, 03:08 PM   PM User | #1
john_w3
New Coder

 
Join Date: Jan 2010
Location: Cardiff, UK
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
john_w3 is an unknown quantity at this point
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.
john_w3 is offline   Reply With Quote
Old 12-03-2012, 03:39 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,640
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 12-03-2012, 04:01 PM   PM User | #3
john_w3
New Coder

 
Join Date: Jan 2010
Location: Cardiff, UK
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
john_w3 is an unknown quantity at this point
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.
john_w3 is offline   Reply With Quote
Old 12-03-2012, 04:18 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,640
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 12-03-2012, 04:50 PM   PM User | #5
john_w3
New Coder

 
Join Date: Jan 2010
Location: Cardiff, UK
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
john_w3 is an unknown quantity at this point
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.
john_w3 is offline   Reply With Quote
Old 12-03-2012, 05:33 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,640
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 12-03-2012, 05:47 PM   PM User | #7
john_w3
New Coder

 
Join Date: Jan 2010
Location: Cardiff, UK
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
john_w3 is an unknown quantity at this point
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?
john_w3 is offline   Reply With Quote
Old 12-03-2012, 05:56 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,640
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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).
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
john_w3 (12-04-2012)
Old 12-04-2012, 02:48 PM   PM User | #9
john_w3
New Coder

 
Join Date: Jan 2010
Location: Cardiff, UK
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
john_w3 is an unknown quantity at this point
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.
john_w3 is offline   Reply With Quote
Reply

Bookmarks

Tags
error_get_last, zend

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:11 PM.


Advertisement
Log in to turn off these ads.