Jump to content

PHP function call architecture


stelios

Recommended Posts

Hi all,

I think this is a pretty basic question, too basic probably... :-[, but here is goes.
Lets say I have:
[b]Function A[/b], returns a value
[b]Function B[/b], doesn't return a value.
If [b]function A[/b] calls [b]function B[/b], will [b]function A[/b] have to wait until [b]function B[/b] terminates or it will continue execution seamlessly?

Thanks
Stelios
Link to comment
Share on other sites

If I understand you correctly you're asking if you can do something like this:

[code]
<?php
function A(){
  B();
  return $somthing;
}

function B(){
  while(true){
    // Do something here in parallel to what's going on in the calling function.
  }
}
?>
[/code]

If so, you're looking to write a multi threaded program.  PHP does not handle this natively, you can probably hack your way through something on a *nix system outside of a webserver using forked processes but it won't be pretty.  My suggestion would be to take a look at Java as it supports multi threading natively.

Best,

Patrick
Link to comment
Share on other sites

ok to explain better the situation is like this:
I have a registration/activation group of functions that obviously send an email to activate the account when the user registers. What I want is to handle the case that the email is failed to be sent. So I thought of having another function running that lets say retries to send the email 10 times and if it fails it registers that into a DB table.
Since that is not possible with php my thought is in case of email failure register that in DB, and have another script(shell script?) called that will retry to send the email x times.
All this is in the purpose of not hacing the user wait all the time it takes to resend the email.

thanks for your time
stelios
Link to comment
Share on other sites

[quote]Since that is not possible with php[/quote]

It is possible with php, but the whole process will need to wait until its finished. eg;

[code]
<?php
  function send_email($email) {
    for ($i=1,$i=10,$i++) {
      if (mail('blah blah')) {
        return true;
      }
    }
    return false;
  }
?>
[/code]

This will attempt to send the email up to 10 times. It will return true as soon as it succeeds or false if it never does. However.... I would not recommend actually using this apprough. Just because mail returns true / false does not meen the email was successfully recieved.
Link to comment
Share on other sites

I guess my question is, if the mail function fails the first n times.  Why would we expect it to work the (n + 1)'th time or the (n + 100)'th time for that matter?  Furthermore is this something that will happen enough to warrant fixing the prolbem programatically or would it be enough to flag the problem (which you are already doing) and resending the email in batch at another time?
Link to comment
Share on other sites

Reply to: thorpe | Reply #7
I know I can do that, but that is what I want to avoid...keeping the user waiting.

Reply to: utexas_pjm | Reply #8
[quote]or would it be enough to flag the problem (which you are already doing) and resending the email in batch at another time?[/quote]
This is what I'm planning to do. In general I can not verify that the email will be sent since it wasn't sent on the first time, but I want to resend it as a first measure and then flag it accordingly for further investigation.

Reply to: btherl | Reply #9
This is the approach I'm testing now. Writing a script perl at the moment. Will let you know how it goes.

Thanks for the help guys
stelios
Link to comment
Share on other sites

yep calling perl form php works:)
PHP doesn't have to wait for the perl script to finish execution.
Although this approach might not be the most efficient. I think "utexas_pjm | Reply #8", having a batch file might be the best idea. Although I will keep running the perl script for now, see how many emails fail and/or if the resend succeds.

very nice to see such an active forum. thanks for the help and I hope I can contribute to the forum at some point...:)
stelios
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.