0

I've this PHP application whose startup is really complicated. I'm inspired from how nodejs works (the application is live and serves clients without load and unload like PHP).

For each request, apache loads the script, instantiate classes and objects (I've so many of them hence startup is resource intensive) and executes it, then unloads it (what a waste), and repeats the same process for the next request.

In Nodejs the script is loaded into memory for the entire session, and code is executed from start to end for each request, there's no loading of script for each request and no unloading at the end of the request.

Is it possible to accomplish the same using linux daemon. I'll be deploying it on AWS Elasticbeanstalk.

Example PHP code

<?php 
echo "Hello " . $_SERVER["REMOTE_ADDR"];
//followed by a series of instantiation of several classes and objects at startup
//would like to have them instantiated only once, keep in memory and then executed
//based on the URL onwards from there
//just like how nodejs works 
?>
  • 1
    If the startup is complicated, I would store the state in a session, database, text file, etc.. However, it seems anything is possible: https://github.com/shaneharter/PHP-Daemon – jeroen Jan 22 '15 at 23:25
  • hi Jeroen, it's not the state, it's actually instantiation of classes and objects and their interlinking, hence the startup is computationally heavy, instead I'd like to have instantiation performed only once and the url based request executed from there, I wonder if nodejs example and how it works gives an idea –  Jan 23 '15 at 12:28
  • It's a lot easier in nodejs as that is basically apache and php in one; it runs it's own web-server, processes requests, etc.. Note that with php and apache, php does not receive http requests, apache does. – jeroen Jan 23 '15 at 12:45
  • got it... so is there a way that we can make apache and php behave like the way nodejs does, I want all my PHP objects to be instantiated only once and then receive url params from apache and start execution from there through an entry point. –  Jan 23 '15 at 12:50
  • Have you checked the link in my first comment? That seems to do what you want. – jeroen Jan 23 '15 at 12:53

1 Answers1

-2

Maybe this page can help you: PHP Command line usage:

php -f somefile.php

then maybe you need to create a bash script and add to the daemon. as explained in this page

Regards

Phoenixzero

Community
  • 1
  • 1
PhoenixzeroX
  • 61
  • 1
  • 9
  • 1
    This executes from the command line; that is not the same as running as a daemon. –  Jan 22 '15 at 23:34
  • @CamilStaps. You're right. Need to expand my answer. Just you need to create a hash script and add it to the daemon. http://stackoverflow.com/questions/19233529/run-bash-script-as-daemon – PhoenixzeroX Jan 22 '15 at 23:38
  • Hi @PhoenixzeroX will it keep the script loaded in the memory, i.e all my object instantation will be performed once or is it going to instantiate objects in the memory everytime for each request –  Jan 23 '15 at 12:30