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

Hello :),


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. :shifty:

Thanks in advance,
Márcio

That’s what you need to incorporate in your logout() function I’d say, not outside of the logout() function.

So, not:


if ($this->loggedIn())
{
  $this->logout();
}

but:


function logout()
{
  if (!$this->loggedIn()) return;
  // actually log out here
}

:slight_smile:

Cool. :cool: :smiley:

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árcio

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árcio

Just a thought: can’t you log out the user before the if/else block? Or would something go wrong if you did that?

This should do what you want:


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

:slight_smile:

What do you return on failure? Nothing?


<?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.

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. :nono:

:wink:

I will study your suggestions later on.

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… :shifty:

Thanks a lot.
Márcio