Results 1 to 7 of 7

Thread: Equivalent to Java finally keyword in PHP

  1. #1
    Join Date
    Jan 2010
    Posts
    126

    Equivalent to Java finally keyword in PHP

    Hello,
    When I create a method to perform queries on my BDD with PDO, I put my code (the connection to the application) in a try / catch. And therefore I would like to close my connection in a finally block to be on it closes even if there is a problem in my code. But I believe that this statement does not exist in PHP5, and I was wondering if there was no equivalent. Thank you in advance for your answers.

  2. #2
    Join Date
    Dec 2009
    Posts
    296

    Re: Equivalent to Java finally keyword in PHP

    Hello,
    You can simply close your connection in your destructor, and in your catch. Thus, you are sure to disconnect your base. But it seems to me that php automatically closes its connection to the database at the end of the script. (Unless otherwise parameter) I am wrong?
    See if this can help you
    Final keyword in Java

  3. #3
    Join Date
    Apr 2008
    Posts
    242

    Re: Equivalent to Java finally keyword in PHP

    In fact, the direction finally does not exist in PHP (for now, who knows what PHP we reserve ). However, as stated Stop, the method __destruct (destructor) is called (if it exists) before deleting the object. To recall, the method __construct (manufacturer) is called after instantiation. The constructor in PHP 4 is the name of the class, as in Java. With versions of PHP 5 + is the magic method __construct. The concept of a destructor does not exist in Java it seems To confirm. In PHP 4 again, destructors do not exist. But you can cheat with the records of functions fences.

  4. #4
    Join Date
    Nov 2009
    Posts
    446

    Re: Equivalent to Java finally keyword in PHP

    If you use PDO to connect you to your core, PDO closes its connection itself before being destroyed. For features specific to each platform, it must be true, but to confirm (because it is different depending on the drivers used). It is always best to do it manually if you're not sure. So put this statement in the destructor is a good idea. But I have a doubt ... the block finally catches all exceptions that have not been caught by a block catch that preceded it. So using finally is not very useful, as you say: "And therefore I would like to close my connection in a finally block to be on it closes even if there is a problem in my code. In this case, no need of a couple try/catch (or triplet try/catch/finally)?

  5. #5
    Join Date
    Jan 2010
    Posts
    126

    Re: Equivalent to Java finally keyword in PHP

    I am not sure what I will say, but it seems that a finally block executes what it had to happen in a script before. In Java this is the way that I submitted it anyway. Because if you just put the closure of your connection just like that as you're behind a try catch, if an error occurs it happens in wrestling and after the code is not executed, and suddenly your connection is not not close. But I retain the idea of putting it in the destructor.

  6. #6
    Join Date
    Dec 2009
    Posts
    192

    Re: Equivalent to Java finally keyword in PHP

    Try the following in your code

    Code:
    try (
    
        / / Attempts.
        $ Ptest = ConnexionPDO::getInstance();
       / / ...
    
    )
    catch ( PDOException $ exc ) (
    
    
        CloseConnBD:closeConnection($ Ptest->getDBH());
        throw $ exc;
    
    )

  7. #7
    Join Date
    Aug 2009
    Posts
    124

    Re: Equivalent to Java finally keyword in PHP

    Check out the following code


    Code:
    try (
        throw new MyException('test', 0);
    )
    
    catch ( AnException $ e ) (
        / / In will not go here ...
    
    )
    catch ( AnOtherException $ e ) (
    
        / / ... Or here.
    
    )
    catch ( Exception $ e ) (
        / / We'll always here if all others fail.
    )

Similar Threads

  1. Final keyword in Java
    By Level8 in forum Software Development
    Replies: 4
    Last Post: 26-10-2011, 08:04 PM
  2. How to use same keyword for two purposes in java?
    By Juaquine in forum Software Development
    Replies: 4
    Last Post: 08-03-2010, 09:48 PM
  3. What is the 'finally' Block in Java?
    By Damien25 in forum Software Development
    Replies: 4
    Last Post: 18-02-2010, 05:31 AM
  4. Difference between final, finally and finalize in Java?
    By Maddox G in forum Software Development
    Replies: 6
    Last Post: 31-01-2010, 11:09 AM
  5. encodeURIComponent equivalent in Java
    By Zidaan in forum Software Development
    Replies: 4
    Last Post: 25-10-2008, 05:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,578,083.29019 seconds with 17 queries