PHP is Weird, Stateless, and Beautiful

Sdílet
Vložit
  • čas přidán 6. 09. 2024

Komentáře • 37

  • @soniablanche5672
    @soniablanche5672 Před 11 měsíci +6

    tl;dr: php is being used as a scripting language by a webserver (apache/nginx/php built-in webserver/etc) , node/python/etc are being used as web servers.

  • @rabin-io
    @rabin-io Před 11 měsíci +21

    I don't think the comparison is really correct, because it kind of matters how you implement the WEB server. PHP can be similar in its behavior to what you showed with the other languages if you use another implementation for example swoole, which creates a permanent server and receives the requests, and not like the internal server of PHP which is like a proxy.

    • @abdelkadermh
      @abdelkadermh Před 11 měsíci +5

      Openswoole is an extension, which alter the PHP server behavior,
      PHP by default is stateless.

    • @andys844
      @andys844 Před 11 měsíci +3

      I agree, it's not the language which is or isn't stateless, it's how you choose to implement it. You can create your own server in PHP by directly listening to a socket, using something like swoole or Ratchet and you'll get persistence because you're choosing to run it as a long running service. All of the other examples here could be run in a stateless way, that's essentially what serverless functions are. Node isn't a language, it's a way to run JS in an event loop and handle HTTP. Flask is a library to enable you to handle HTTP in python. PHPs design philosophy has always been to allow a shared nothing environment but that doesn't mean you can't run it a different way.

  • @static-san
    @static-san Před 11 měsíci +3

    Being able to consider each HTTP request to a PHP server as "running a new copy of the program" meant that you got some scalability almost for free. I remember working on a website that had 20 PHP webservers behind a load balance and all persistent storage was in a MySQL database. And most of the other programmers didn't have to worry about _how_ it scaled. (I did, though - I was the DBA and website administrator.)
    I remember when Java started muscling in to the web server app scene, it had a very different execution model, much more like Go or node do now. Scalability was actually harder, persistent storage was more complicated and programmers had to understand how running the web server app on load-balanced web servers was subtly different than on just one.

    • @dubble
      @dubble Před 11 měsíci +2

      It also means that you're doing a lot of bootstrap on every request, which is very slow.
      That's the reason why modern webservers for php often make use of workers - starting your php application and not shutting it down after a request. Examples: Swoole, Roadrunner, FrankenPHP (Caddy) and more.
      Point being, php isn't stateless. The common old-school implementations of php apps being served with apache/nginx + php-fpm is.

    • @static-san
      @static-san Před 11 měsíci

      @@dubble Right. I remember when Apache integration started experimenting with that. There were also Java frameworks that did that years ago but the hand off to the request thread was not real clean and you still had to know what was going on. At least the PHP ecosystem comes to that with the awareness that years of programmers haven't had to worry about how that works.

    • @lawrencedoliveiro9104
      @lawrencedoliveiro9104 Před 11 měsíci

      Also means PHP cannot do WebSockets.

  • @ryanvelbon
    @ryanvelbon Před 3 měsíci

    Great explanation. No fluff. No boilerplate cluttering the examples.

  • @barneylaurance1865
    @barneylaurance1865 Před 10 měsíci +3

    PHP doesn't have to make a new TCP connection to the database for every web request. Although all the variables you define in your PHP code are share-nothing, the PDO database extension does have a built in feature to persist that connection from one request to another - you have to pass the PDO::ATTR_PERSISTENT option when setting up the connection, and then it will re-use an existing pooled connection if possible.
    It doesn't mean you get to continue any transactions from a previous request or anything like that, but you do get to avoid creating a new TCP connection from the web server to the database server.

  • @GRHmedia
    @GRHmedia Před 11 měsíci +3

    cgi/perl was popular prior to PHP. PHP took off because it was easier and had better performance.
    PHP has relatively good performance to start with. There a number of solutions were you can easily improve site performance with relative easy. But what is nice about PHP is you can crease a C/C++ module for it as easy as I can for python or node. ... Yet, you still get the ease of using PHP's stateless behavior. If I need better performance than that I might as well drop to a compiled language like C/C++.

  • @TonyMessias
    @TonyMessias Před 11 měsíci +9

    PHP is awesome.

  • @minor12828
    @minor12828 Před 11 měsíci

    The frameworks used has a type of application context which is a singleton. Can you please try with a PHP framework ?

    • @fideloper
      @fideloper  Před 10 měsíci +1

      Hi! - I did use a PHP framework (Laravel). PHP is just different historically in how it runs. However there is Laravel Octane, which runs as a long running process (with the help of Swoole or RoadRunner)

  • @jfdirienzo
    @jfdirienzo Před 11 měsíci +1

    I don't think this comparison is really fair. In all the other languages, you encapsulated both the domain logic (increase a counter) and the http server, thus making it a program (an infinite loop) whereas in php you only used the langage to write a script responding to an external web server. Php is not stateless by essence, a script is. If you decided to write the http server in php as well, you would have write a php program (in opposition to a script) and then, php would have behaved statefully. It's true in the other hand that php is most commonly used as a scripting language in conjunction with a server like Nginx or Apache because the langage is both single threaded and hardly asynchronous (even though fibers and generators are a workaround this issues)

    • @fideloper
      @fideloper  Před 10 měsíci +1

      Right! I mean - what you said is all accurate, but that's not how PHP apps are run (mostly!). They're run as scripts. That being said: Laravel Octane DOES run more like the other frameworks ("as a program") where you need to care about state.
      I actually wish PHP was more like the other frameworks in some respects, it makes doing things like containerizing PHP apps easier. I really wish PHP "spoke http" natively, but that would require a lot of changes to PHP (native async type things).

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 Před 11 měsíci

    PHP needs something like Python ASGI. But that in turn requires that it have something like asyncio. Which in turn is built on async/await.
    Moral: it takes a lot of good decisions to end up with a good language. Adding features piecemeal doesn’t take you there.

  • @antonyjere
    @antonyjere Před 11 měsíci

    are you thinking of creating a modern paid devops course(not e-book) for laravel/php world? With things that companies ask for, that would be so great, since your explanations rock.

    • @fideloper
      @fideloper  Před 11 měsíci +2

      thanks! I’m trying to cook up ideas for a course for sure. What would you like to see?

    • @antonyjere
      @antonyjere Před 11 měsíci +1

      @@fideloper It would really make sense to see different job posts from real companies and create a course that covers those things. I have seen multiple times: Basic deployments (php+nginx , the old workable classic way). Docker for sure. Kubernetes is something that really can go the thing to the next level, since many companies ask for it, and this can definitely increase developer's value, being able to get a better offer from the company. What else hmm, scaling stuff are being asked too, load balancing, etc. Job posts can really guide you on that I guess. I just wrote a very short description. That's because after 5 years of working as a PHP/Laravel developer, mostly on big Product Startup companies, I still struggle with these. Having a single course that just explains the things, would be great.

  • @developerkwame
    @developerkwame Před 11 měsíci +1

    With any Async PHP implementation, it will work just as the other languages. So as PHP fibres exists it can do same or maybe I'm wrong.
    But this is a great video showing that PHP is truly stateless

    • @SXsoft99
      @SXsoft99 Před 11 měsíci

      fibres should be used for scripting and async code execution like cronjobs and queue jobs, you don;t want to stick your main thread

  • @thomaswatling
    @thomaswatling Před 11 měsíci +2

    This is a nice explanation! It explains why I found Python running in serverless functions easy to get my head around with past PHP knowledge.

  • @WantMore-mc8dx
    @WantMore-mc8dx Před 11 měsíci

    Http is stateless if u choose so. With any kind of tokens passed it can keep a state (cookies/querystring/htm). And PHP can handle them all. PHP is just a server side language and can behave the same as any other language. Connection pooling isn't a memory for a request - normally just to keep connections to database more efficient. PHP is ok in most aspects! /regards from a mainly c# developer

    • @rcnhsuailsnyfiue2
      @rcnhsuailsnyfiue2 Před 11 měsíci +2

      Passing tokens is not the same as being “stateless”. Until the token is read and parsed, there is no continuity of state between each request. It’s a subtle distinction but fundamental in the context of this video :)

    • @lawrencedoliveiro9104
      @lawrencedoliveiro9104 Před 11 měsíci

      Having a new execution context created for every request is called “inverted programming”. In many cases, it can be more natural to have a continuous context where the code flows sequentially from one request/response to the next.
      Also, this is essential for handling things like WebSockets. Which PHP can’t do.

    • @jaimelannister141
      @jaimelannister141 Před 9 měsíci

      @@lawrencedoliveiro9104 PHP also offers a different model of execution called Swoole which supports Web Sockets, among many other things. This video is about PHP-FPM more than it's about PHP in general.

  • @webhooktips
    @webhooktips Před 11 měsíci +1

    Yes

  • @clementborisov7050
    @clementborisov7050 Před 11 měsíci +1

    I've been a proud user of php since 2008

  • @fmkoba
    @fmkoba Před 11 měsíci

    yep, great

  • @rahul_bali
    @rahul_bali Před 9 měsíci

    why I couldn't understand this rambling.