SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru
    Join Date
    Feb 2009
    Posts
    994
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Logic + basic programming issue help. Execute something after a return;

    Hello ,

    PHP Code:
    if ($codigo == 1000)
                {

                    
    $dadosCriados $resposta->response->resData->children("urn:ietf:params:xml:ns:contact-1.0")->creData;

                    
    $mensagem['id'] = $dadosCriados->id;
                    
    $mensagem['dataCriacao'] = $dadosCriados->crDate;
                    
                    return 
    $mensagem;

                }
                else
                {
                    echo 
    "Erro - O contacto nao foi criado - A resposta foi: ".$codigo." - ".$result->msg;
                }

           
                
              
    $this->logout(); 
    I have a logic issue that I would like to ask your help for.
    On this code, I would like to return $mensagem but, as well, at the end, do the logout.

    Right now, if I return $mensagem, the block execution ends, and nothing else is executed (I believe that this is the expected behavior). I cannot put the logout near the return $mensagem and before it because, it needs to be done, even if an error is returned.
    Also, I cannot put return $mensagem; near the $this->logout(); line, because, if I do this, the $mensagem will be returned, even if an error occours, that we don't want that...

    It must be something really dummy.


    Thanks in advance,
    Márcio

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,892
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    This should do what you want:

    PHP Code:
    <?php
    $mensagem 
    = array();
    if (
    $codigo == 1000)
    {
        
    $dadosCriados $resposta->response->resData->children("urn:ietf:params:xml:ns:contact-1.0")->creData;

        
    $mensagem['id'] = $dadosCriados->id;
        
    $mensagem['dataCriacao'] = $dadosCriados->crDate;

        return 
    $mensagem;
    }
    else
    {
        echo 
    "Erro - O contacto nao foi criado - A resposta foi: ".$codigo." - ".$result->msg;
    }

    $this->logout();
    if (
    count($mensagem) > 0)
        return 
    $mensagem;
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What do you return on failure? Nothing?

    PHP Code:
    <?php
    function stuff($codigo){
        
    $status null;
        if(
    1000 === $codigo){
            
    $status = array(
                
    'id'    => '',
                
    'data'  => ''
            
    );
        }else{
            echo 
    'error';
        }
        
    $this->logout();
        return 
    $status;
    }
    Also, there is no harm in calling $this->logout() in the else block.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Guru
    Join Date
    Feb 2009
    Posts
    994
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you please take at least like... 5 minutes answering... otherwise, it would seem to obvious and, as a result, I would seem to dummy.



    I will study your suggestions later on.

    Quote Originally Posted by AnthonySterling View Post
    Also, there is no harm in calling $this->logout() in the else block.
    Yes... it's what I have right now, two calls to the logout() one on the first part of the conditional, another in the else part of it.

    If no one will loud on me because of this, it's a quite practical solution...

    Thanks a lot.
    Márcio

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,892
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Just a thought: can't you log out the user before the if/else block? Or would something go wrong if you did that?
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    SitePoint Guru
    Join Date
    Feb 2009
    Posts
    994
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Just a thought: can't you log out the user before the if/else block? Or would something go wrong if you did that?
    Ups... sorry... my last reply was not correct, you say before... hm... Yes I believe I can, since each block of commands does their own login (how worst can I make it? :s).
    However, I still need to verify if the login is in place, and if it is, then logout.

    Regards,
    M&#225;rcio

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,892
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by oikram View Post
    I can make logout at the very end but, I would need to do so, only if a login has been made...
    That's what you need to incorporate in your logout() function I'd say, not outside of the logout() function.

    So, not:
    PHP Code:
    if ($this->loggedIn())
    {
      
    $this->logout();

    but:
    PHP Code:
    function logout()
    {
      if (!
    $this->loggedIn()) return;
      
    // actually log out here

    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  8. #8
    SitePoint Guru
    Join Date
    Feb 2009
    Posts
    994
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool.

    It makes all the sense, because there is no such thing as a logout of something else that is not a login, hence, the verification can well stay on the logout function itself.

    Thanks.
    Is there a theoretical name for this sort of operation or it is something that practice will provide us?



    M&#225;rcio


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
  •