1

Is this achievable? If not, what are other ways in doing it best?

I'm trying to:

  1. Return true after a successful function call is made.
  2. Let the called function still execute even after return statement.

To make things clearer:

function foo() {
  var $data; // a very large data
  var $check = $this->bar($data); // gets true
  if ($check) {
    echo "FOO: Bar received my message!";
  }
}

function bar($data) {
  return true; // returns true, now foo doesn't care what bar will do
  echo "BAR: returned true to foo, will now call foobar!";

  // some code to process $data

  $this->foobar(); // but hopefully still execute this line
}

function foobar() {
  echo "FOOBAR: Hello, World!";
  return;
}

Expected output: (asynchronously)

FOO: Bar received my message!

BAR: returned true to foo, will now call foobar!

FOOBAR: Hello, World!

Hard Spocker
  • 765
  • 11
  • 32
  • You could try something with forking, but I don't see any benefit to this... – Niet the Dark Absol Nov 04 '15 at 18:36
  • i am thinking you should nest multiple functions one inside the other. One function for each value to be returned. The container function will return the collection of all the values returned from the functions inside. But this is not imho a good way to do it and probably you can rethink your logic to get a single code to do everything – Lelio Faieta Nov 04 '15 at 18:38
  • What you are looking for is called "asynchronous" processing. Such thing exist in some languages, notably javascript, but not natively in php. There are some extensions, but not really widespread. To suggest an alternative approach (which certainly does exist), you would have to explain what it actually is you want to do. So not _how_ you might do that, but what and why. – arkascha Nov 04 '15 at 18:40
  • Just so you know, what I'm actually doing is I want to send a large data to a function and process it. And rather waiting for that function to finish processing the data, I will just return true (and say yeah I got the data and will work on it. you don't have to wait ill send you a notification after i finished processing) @arkascha – Hard Spocker Nov 04 '15 at 18:42

2 Answers2

2

What you are looking for is called "asynchronous" processing. Such thing exist in some languages, notably javascript, but not natively in php. There are some extensions, but they are not really widespread since this is a bit against the idea and strength of php.

Typically two alternative approaches are used, where the second option is by far the better one:

  1. you send a normal response to the client and use phps ignore_client_abort feature. That feature allows to continue processing even after having sent a response. Note however that processing a higher amount of data in such synchronous manner is questionable: you most likely will run into some of the many php limits implemented for safety reasons. So yu would have to raise all those limits, which lowers your protection against miss use and bugs.

  2. you use a job scheduler to store data processing jobs and notify the client about the fact that you received the data and created the processing jobs. Then you process each job in the queue one by one by means of a cronjob or similar, so independent of a client request. You get extra points if you implement a view on the queue state for the client :-)

    There are frameworks for such things, basically what they do is to create entries in a database, one for each job. Then you have a single cron job fired periodically (every minute). That job checks if another job is already getting processed, if not it picks the next one and starts processing. Usually processing is done per php-cli calls to be independent from the web server environment. That also means you can even execute the jobs on a different system for a better performance. You have decoupled the processing from the web gui.

Community
  • 1
  • 1
arkascha
  • 41,620
  • 7
  • 58
  • 90
1

If you want to run more functions at the same time, PHP supports multi-threading. Here is a simple yet great example of what it can do: How can one use multi threading in PHP applications

Also, see http://php.net/manual/en/class.thread.php

Community
  • 1
  • 1
empir07
  • 33
  • 3