1

From my understand Composer is used to autoload classes via the SPL function provided by PHP, or at least register the method to call when the class does not exist. This then has to happen upon every request for a traditional setup with Laravel or CakePHP for example...

My question is, how would Composer work in a Swoole HTTP Server situation where you are free to preload everything before hand? Is Composer even needed in this context?

A Swoole HTTP PHP Server in basic terms looks like this:

<?php

// Load all your classes and files here?

$http = new swoole_http_server("127.0.0.1", 9501);

$http->on("start", function ($server) {
    echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

$http->on("request", function ($request, $response) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World\n");
});

$http->start();

So I could load everything before hand not worry about having to call any autoloading script?

All of the classes would then be in global scope thus, everything is preloaded and ready to use in the ->on("request") function callback.

tty2018
  • 45
  • 1
  • 6
  • Yes, you could. Just like you could do it in any other PHP project. But it's still much more convenient to let the autoloader take care of everything. Without one you are back to managing require statements by hand. – Peter Apr 30 '19 at 11:33
  • @Peter Ok but doesn't composer only load classes when they are called? Or can Composer load all classes at once? – tty2018 Apr 30 '19 at 11:37
  • Yes, classes are loaded if and when needed (because [that's how autoloading works](https://php.net/manual/en/language.oop5.autoload.php)). – Peter Apr 30 '19 at 12:06

1 Answers1

1

You can use composer and its autoloading feature in CLI context, with Swoole.

There's no change in to PHP's execution, so autoloader will work just fine, just include the vendor/autoload.php at relevant script.

<?php

// Autoloader is now up, you can use new Your/Class;
require_once('vendor/autoload.php'); 

$http = new swoole_http_server("127.0.0.1", 9501);

$http->on("start", function ($server) {
    echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

$http->on("request", function ($request, $response) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World\n");
});

$http->start();

Disclaimer: I use swoole with Laravel, Lumen and custom solutions (both CLI and fastcgi)/web, it works great and there's no change in how you work with PHP in that context.

Mjh
  • 2,904
  • 1
  • 17
  • 16
  • 1
    But doesn't that mean the classes are loaded when they are needed? I'm on about preloading everything first. So before the PHP CLI Swoole server starts every class has been defined for PHP. It would be better to preload everything first, right? – tty2018 Apr 30 '19 at 12:09
  • 2
    Yes, it would load the class when needed. If you preload, you're not achieving anything - when class is needed, it's loaded *if not loaded already*. All subsequent calls to the class file itself won't force PHP to load it again. Therefore, if you did manage to preload everything - you're not really solving anything as the classes are loaded only once. Basically, you'd have one request that'd be used to hot-start everything. Alternative is to create a list of all classes, foreach it and manually `require_once` each file. Which is what composer does for you. – Mjh Apr 30 '19 at 12:40
  • 1
    I understand your explanation. I think I may have explained my side poorly. When I say preload compared to Composers autoloader, it will only `require` if it is called and not defined, leaving Composer to load a class. But If I were to preload all classes before I start the Swoole server, Composer is not needed or not useful in this context because it does not load all classes/ dependencies at once? – tty2018 Apr 30 '19 at 12:48
  • 2
    If you were to preload everything on your own, then there's be no need for composer. Even if you left its autoloader in your code - it wouldn't do a thing since all the classes are available. However, composer is not just an autoloader generator - you'd *probably* use it to define dependencies for your swoole project. However, you will not gain anything by doing so. There's no performance gain, all that'll happen is you wasting (a lot) of time. Use composer's autoloader. Unless, you really, really want to do it on your own and manually :) – Mjh Apr 30 '19 at 13:05