PHP has come far and continues to strengthen with new options, syntax, and velocity. The ecosystem could also be increasing, with many builders growing frameworks to simplify the lives of different builders. Widespread, full-featured frameworks like Laravel and Symfony exist, as do light-weight microframeworks like FrameworkX.

This can be a light-weight microframework for PHP that makes use of an event-driven, non-blocking structure, very similar to Node.js which is best possible for high-concurrency and real-time programs like chat apps or reside notifications.

On this article, we can discover what FrameworkX is and the way it differs from conventional PHP frameworks. Let’s get began!

Getting Began

First, you’ll want to have PHP and Composer arrange to your pc. As soon as put in, you’ll upload FrameworkX for your venture with this command:

composer require clue/framework-x

FrameworkX doesn’t require a fancy setup. All you want is a public/index.php record. Right here’s a fundamental instance to show “Hi Global!” at the homepage:

get('/', fn () => ReactHttpMessageResponse::plaintext("Hi international!n"));
$app->run();

To run your software, kind:

php public/index.php

This command begins a neighborhood server the usage of PHP’s integrated server, sponsored by way of the ReactPHP Socket element. No use for Nginx or Apache. Your server will run at http://127.0.0.1:8080, and it will have to show “Hi Global!”.

Localhost server setupLocalhost server setup

But even so undeniable textual content, you’ll additionally go back JSON knowledge. As an example:

 'Jon Doe'], ['name' => 'Jane Doe']];

$app = new FrameworkXApp();
$app->get('/', fn () => ReactHttpMessageResponse::json($customers));
$app->run();

Async Operations

PHP operations are normally blocking off and synchronous, which means every activity will have to end earlier than the following one begins. FrameworkX is constructed at the ReactPHP library.

ReactPHP is a library that gives elements such because the EventLoop, Circulation, Promise, Async, and HTTP elements, which allow asynchronous operations. Thus, duties can run similtaneously with out looking forward to others to complete. That is splendid for dealing with more than one connections, HTTP requests, or I/O operations concurrently.

On this instance, we’ve up to date our index.php to fetch an API. As a substitute of the usage of the curl_* purposes, we can use the HTTP element to make an asynchronous request.

$app = new FrameworkXApp();
$app->get('/', serve as () {
    echo "Startn";
    (new ReactHttpBrowser())
        ->get('https://www.hongkiat.com/weblog/wp-json/wp/v2/posts')
        ->then(serve as () {
            echo "Finish (API)n";
        });
    echo "Endn";

    go back ReactHttpMessageResponse::plaintext("Hi international!n");
});
$app->run();

Most often, an exterior API request would block the web page from rendering till the request completes. Alternatively, with the asynchronous operations that the ReactPHP HTTP element handles, the web page rather a lot in an instant, as evidenced by way of the log.

Async operations logAsync operations log

This makes FrameworkX able to dealing with many extra concurrent requests than conventional PHP setups, considerably rushing up web page load instances. However how briskly is it?

Velocity

I examined FrameworkX on a fundamental, affordable DigitalOcean droplet with 1 vCPU and 1GB of RAM. It treated round 4,000 requests in line with 2d easily.

Concurrency Degree:      50
Time taken for checks:   22.636 seconds
Entire requests:      100000
Failed requests:        0
Stay-Alive requests:    100000
General transferred:      17400000 bytes
HTML transferred:       1300000 bytes
Requests in line with 2d:    4417.69 [#/sec] (imply)
Time in line with request:       11.318 [ms] (imply)
Time in line with request:       0.226 [ms] (imply, throughout all concurrent requests)
Switch charge:          750.66 [Kbytes/sec] gained

Even with further workload, like disk learn operations and rendering 100 lists from a JSON record, it nonetheless controlled round 2700 requests in line with 2d.

Concurrency Degree:      50
Time taken for checks:   36.381 seconds
Entire requests:      100000
Failed requests:        0
Stay-Alive requests:    100000
General transferred:      296700000 bytes
HTML transferred:       280500000 bytes
Requests in line with 2d:    2748.72 [#/sec] (imply)
Time in line with request:       18.190 [ms] (imply)
Time in line with request:       0.364 [ms] (imply, throughout all concurrent requests)
Switch charge:          7964.31 [Kbytes/sec] gained

I’m lovely certain it might be a lot quicker with higher server specs.

Wrapping Up

FrameworkX is an impressive, light-weight microframework for PHP. It runs asynchronously and is able to dealing with more than one duties successfully, very similar to Node.js. It’s the easiest framework whether or not you’re construction a easy app or complicated high-concurrency or real-time programs.

The submit Creation to FrameworkX gave the impression first on Hongkiat.

WordPress Website Development Source: https://www.hongkiat.com/blog/frameworkx/

[ continue ]