1

Is there any way to make a php file always running and accessible from others php files/requests?

I want to make a functions, objects, variables or some others things always running on the server.

Example:

[alwaysrunning.php]

$conn = fsockopen($serverip, $serverport, $errno, $errstr, $ex);

[index.php]

fwrite($conn, $command."\n");

Sorry my english and thank you!

Nurio Fernández
  • 518
  • 5
  • 22
  • Include your functions in a file (somefunctions.php). Then in your index.php file, include them: `require_once('...PATH.../somefunctions.php');` Then call the functions required to initiate connections and others. Look at this: https://stackoverflow.com/questions/4458837/how-to-define-global-functions-in-php – Nic3500 Apr 16 '18 at 14:47
  • @Nic3500 i don't want include, i want that variable never die, always running, like in other thread. – Nurio Fernández Apr 16 '18 at 14:48
  • 2
    What problem are you actually trying to solve? There's no technical reason this can't be done, but it's definitely not a common pattern. – iainn Apr 16 '18 at 14:53
  • I wonder how to use that file without including. A singleton pattern might help you – Cid Apr 16 '18 at 14:53
  • 4
    PHP is inherently stateless, the request starts, the caller receives a response and that's that. If you want to maintain state PHP is not the answer. If you want to have access to it in the current request(something like a global state), that's a different matter. – Andrei Apr 16 '18 at 14:53
  • 1
    Read this one, very detailed! https://stackoverflow.com/questions/14268018/concurrent-use-of-a-persistent-php-socket and http://php.net/manual/en/function.pfsockopen.php. Seems feasible after all! – Nic3500 Apr 16 '18 at 14:56
  • @Andrew are you suggesting me to use another language? Which one would you recommend? – Nurio Fernández Apr 16 '18 at 15:05
  • Depends on what you want to achieve here. I don't want to suggest languages just because. Maybe if you'd explain in greater detail what you're aiming for I'm sure people would come up with a more suitable result. – Andrei Apr 16 '18 at 15:06
  • @Nic3500 i used 'fsockopen' as an example, but thanks anyway. :) – Nurio Fernández Apr 16 '18 at 15:07
  • @Andrew & iainn :: I'm not doing anything in particular, I'm just looking for ways to make code more efficiently due to the fact that connections are usually the bottleneck of my systems. – Nurio Fernández Apr 16 '18 at 15:11
  • 1
    Well in that case a different language is not the answer. Even if you kept a persistent connection it's still a connection. And PHP does offer that too [pecl_http](https://pecl.php.net/package/pecl_http). If you want to have access to the information in the file on a regular basis nginx is really good at serving static content or just use a CDN and solve the connection to your system all together. – Andrei Apr 16 '18 at 15:15
  • 1
    *"I'm just looking for ways to make code more efficiently due to the fact that connections are usually the bottleneck of my systems"* you should have mentioned that in your question , you are a victim to [XY questions](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – Accountant م Apr 16 '18 at 15:19
  • @Accountantم What I am looking for is to be able to execute tasks in a main thread and that different connections/requests can access and modify them, not only make connections. – Nurio Fernández Apr 16 '18 at 15:25
  • @Andrew What I am looking for is to be able to execute tasks in a main thread and that different connections/requests can access and modify them, not only make connections. – Nurio Fernández Apr 16 '18 at 15:26
  • @xXNurioXx AFAIK PHP is designed for request-response pattern. Check [this question](https://stackoverflow.com/questions/9469870/how-do-real-time-strategy-games-work-in-php#comment11982999_9469870), it might help – Accountant م Apr 16 '18 at 15:29
  • @Accountantم i'm not trying to code real time update site, i just want to increase the server performance, and minimize the response time. My question is not how to solve any problem, I am asking if what I want currently has a form. – Nurio Fernández Apr 16 '18 at 18:28
  • @xXNurioXx Please don't try to fix something that has nothing to do with performance and waste your resources. You **must profile** your software to know where exactly are the bottlenecks. May be it is just a SQL query that needs an optimization, or may be it's the server network infrastructure and your code doesn't need anything. However take a look at [PHP opcode cashing](https://stackoverflow.com/questions/2296978/what-is-the-easiest-way-for-opcode-caching-with-php-apache?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – Accountant م Apr 16 '18 at 19:03

0 Answers0