How does PHP work and what is its architecture?

Guys I recently decided to go back to PHP and do a few more complicated things than a simple login page. For 3 years I have been programming Java / JavaEE and am well versed in Java application architecture. Basically, a virtual machine (a simple OS process) that executes compiled code called byte code. a simple Java web server is basically a Java application that listens on the provided TCP port for Http requests and accordingly answers that it is more complicated, but this is its initial work.

Now what about PHP? How it works? Which, in a nutshell, is its architecture.

I searched for this question, but 90% of the articles explain how to implement and build a web application with PHP, which is not what I'm looking for.

+12
source share
3 answers

The biggest difference between Java web server and PHP is that PHP does not have its own embedded web server. (Well, new versions, but they should only be for testing, this is not a ready-made web server). PHP itself is basically one executable file that reads in the source code file of the PHP code and interprets / executes the commands written on this file. It. This PHP architecture in a nutshell.

This executable supports the default API, which can call the custom field PHP code, and extensions can be added to provide more APIs. These extensions are usually written in C and compiled with the PHP executable during installation. Some extensions can only be added by recompiling PHP with additional flags, others can be compiled against installing PHP and activated via the configuration file after the fact. PHP offers the PEAR and PECL side projects as an attempt to standardize and simplify such installations after the fact. Userland PHP code often also includes additional third-party libraries that are simply written in PHP code. The advantage of C-extensions is their speed of execution and access to low-level system access, the advantage of user code libraries is their trivial inclusion. If you manage your own PHP installation, it is often simple enough to add new PHP extensions; however, on a very popular model with a shared host, tension often arises between what the host wants to install and what the developer needs.

In practice, a web service written in PHP runs on a third-party web server, very often Apache that processes any incoming requests and calls the PHP interpreter with the requested PHP source file as an argument and then provides any output for this, go to HTTP to the customer. It also means that a persistent PHP process does not work continuously with a persistent state, as is usually the case in Java, but each request is processed by starting and then flushing a new instance of PHP.

Although Java simply stores persistent data in memory, the persistence of data between requests in PHP is handled using a number of methods such as memcache, sessions, databases, files, etc .; depending on the specific needs of the situation. PHP has operation code cache add-ons, such as Java bytecode, so PHP does not have to repeat the same parsing and compilation process each time it executes the same file.

Keep in mind that it’s possible to write a permanent PHP program that works just like Java, it’s just not the default PHP modus operandi. Personally, I am a fan of writing workers for specific tasks in Gearman or ZMQ that work hard and have some ephemeral scripts running on a web server, like "frontend", which delegate the work to these workers as needed.

If this sounds like a typical PHP application, it is much more like gluing together a few disparate components, you would be right. Java is pretty autonomous, with the exception of external products such as RDBMS servers. PHP, on the other hand, often tends to rely on a bunch of third-party products; which can work to your advantage in the sense that you can use best-in-class products for specific tasks, but also requires large costs for working with various systems.

+24
source

Here's how PHP works:

(one of the best on the internet)

Php internals

+18
source

In general terms, PHP as an engine interprets the contents of PHP files (usually * .php, although alternative extensions are sometimes used) into an abstract syntax tree. The PHP engine then processes the translated AST and then returns the result, taking into account any input and processing that is required.

Below image will display more information

enter image description here

Source: freecodecamp.org

0
source

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


All Articles