PHP architecture: how to do it?

I need help understanding the inner workings of PHP.

Remember, in the old days we used TSR (Terminate and stay resident) (Pre-windows era) routines to write? As soon as this program is executed, it will remain in memory and can be re-executed using the hotkey (the alt- or ctrl-key key combination).

I want to use a similar concept in web server / applications. Let's say I have common_functions.php, which consists of common functions (such as Generate_City_Combo () or Check_Permission () or Generate_User_Permission_list () or the like) for all web applications running on this apache / php server.

In all modules or applications php files I can write:
require_once (common_functions.php);

which will include this shared file in all modules and applications and works fine.

My question is: how does php handle this internally?
Let's say I have: Two apps, AppOne and AppTwo.
There are two menu options in AppOne: AppOne_Menu_PQR and AppOne_Menu_XYZ
AppTwo has two menu options: AppTwo_Menu_ABC and APPTwo_Menu_DEF

All four menu items call functions {for example, Generate_City_Combo () or Check_Permission () or Generate_User_Permission_list ()} from common_functions.php

Now, consider the following scenarios: A) User XXX logs in and clicks on AppOne_Menu_PQR from his personalized dashboard, then looks at all screens and instructions. This is a series of requests for 8-10 pages (screens), and it is interactive. After that, user XXX clicks on AppTwo_Menu_DEF from his personalized dashboard and again, as before, he views all screens and instructions (about 8-10 pages / screens). Then user XXX disconnects.

B) User XXX logs in and does everything that is mentioned in scenario A. At the same time, user YYY also logs in (from some other client machine) and performs the same actions mentioned in scenario A.

For scenario A, this is the same session. For scenario B, there are two different sessions.

Suppose all menu options call Generate_User_Permission_list () and Generate_Footer (), or many menu options call Generate_City_Combo ().

So how many times did PHP execute / enable common_functions.php on the page request? per session? or to start / end PHP? My understanding - common_functions.php will be executed after EVERY page of request / loop / load / screen, right? In principle, once for each interaction.

Remember that functions such as Generate_City_Combo () or Generate_Footer () produce the same output or do the same regardless of who or when is calling.

I would like to limit this once at startup and termination of the application.

These are just examples. My actual problem is much harder and harder. In my applications, I would just like to call Application_Startup () procedures only once, which will create an ideal environment (for example, all search and reference data structures, read-only data, security matrix, menu options, context-sensitive business execution logic, etc. d ..). After that, all requests arriving at the server should not waste time or resources on creating an environment, but can instantly refer to an "already created environment".

Is this something possible in PHP? How? Could you point me somewhere or some books that explain the inner workings of PHP?

Thanks in advance.

+4
source share
4 answers

PHP processes each HTTP request in a completely separate execution frame - there is no ongoing process to serve all of them. (Your web server is running, but each time it loads a PHP page, a separate instance of the PHP interpreter is called.)

If the time required to create the required constant areas is significant, you may want to cache the output from these scripts on disk and first download the cached version if available (and not outdated).

+2
source

PHP (in almost all cases) is page oriented. There is no Application_Startup () application that will support state through HTTP requests.

Sometimes you can emulate this by loading / unloading serialized data from a database or $ _SESSION, but there is overhead. In addition, there are other cases where the memcached server can optimize this, but you usually cannot use the ones you have typical virtual hosting services like cPanel.

If I needed to create an application, as you say, I would serialize the selection of users in the session, and then save everything that is needed to save between sessions in the database.

There are several ORM modules for PHP, such as Doctrine , that simplify serializing objects in a database.

+1
source

I would say that you are probably optimizing prematurely, but there is hope.

You very often want to get several copies of your compiled code in memory, because you need stability for each request; you don’t want individual queries to run in the same memory space and be at risk of race conditions or data corruption!

However, many PHP Accelerators , where the PHP code will be pre-compiled, enable and make calls much faster.

+1
source

I'm necromancing here, but with the advent of PThread, it seems that it might be possible to strike in the direction of the actual solution for this, and not just say, in essence, "No, you cannot do this with PHP."

A user could create their own multi-threaded web server in PHP, only using the CLI tools, socket_ * functions and PThreads. Just listen on port 80, add requests to the request queue, and start a number of worker threads to process the queue.

The number of workers can be controlled based on the length of the request queue and the length of the operating system execution queue. Every few seconds, the main thread could go through a function to control the size of the working pool. If the length of the web request queue was longer than some constant ones, the operating system queue queue duration and the number of workers were less than the configured maximum, this could create an instance of another workflow. If the length of the web request queue was less than some other (lower) constant times when the length of the OS execution queue and the number of workers were more than the configured minimum, he could say that one of the workflows dies when it finishes its current request. Then, the constants and configured values ​​can be tuned to maximize the entire throughput for the server. Something like that.

You will need to do all your own uri analysis, and you will need to compile your HTTP response, etc. yourself, but workflows can create objects that extend Threaded, or reuse previously created Threaded objects.

Voila - PHP TomCat.

0
source

Source: https://habr.com/ru/post/1302565/


All Articles