Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jan 2010
    Location
    Cardiff, UK
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #3
    New Coder
    Join Date
    Jan 2010
    Location
    Cardiff, UK
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #5
    New Coder
    Join Date
    Jan 2010
    Location
    Cardiff, UK
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #7
    New Coder
    Join Date
    Jan 2010
    Location
    Cardiff, UK
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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).

  • Users who have thanked Fou-Lu for this post:

    john_w3 (12-04-2012)

  • #9
    New Coder
    Join Date
    Jan 2010
    Location
    Cardiff, UK
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •