Hacker News new | ask | show | jobs
by krisrm 1660 days ago
Elegant is not the word I'd use to describe PHP. Solid, perhaps. Undoubtedly useful. But if there is elegant PHP, I haven't seen it.
5 comments

Have you looked at any Laravel code? As someone who has worked from small startups to FAANG and inbetween, Javascript to PHP to Python to Go, I haven't found anything more elegant.
I have. Elegant it is not. Actually, it smells like Enterprise Java code, which made me flee from the Java ecosystem. To each his own, but this is not an elegant event listener (copied from the examples page):

  use App\Events\ArtistInformationFetched;
  use App\Services\MediaMetadataService;
  use Throwable;
  
  class DownloadArtistImage
  {
      private MediaMetadataService $mediaMetadataService; 
  
      public function __construct(MediaMetadataService $mediaMetadataService)
      {
          $this->mediaMetadataService = $mediaMetadataService;
      }
  
      public function handle(ArtistInformationFetched $event): void
      {
          $info = $event->getInformation();
          $artist = $event->getArtist();
  
          $image = array_get($info, 'image');
  
          if (!$artist->has_image && $image && ini_get('allow_url_fopen')) {
              try {
                  $this->mediaMetadataService->downloadArtistImage($artist, $image);
              } catch (Throwable $e) {
              }
          }
      }
  }
Source: https://laravelexamples.com/example/koel/events-listeners
It's interesting to me how to show your example you chose something from a 3rd party, non Laravel site (just because it has a laravel in the name doesn't), and chose something which inherently doesn't follow PHP or laravel coding standards (snake_case variables, for instance), when you could easily pull something from the main Laravel website?

https://laravel.com/docs/8.x/container#introduction

Or, if you wanted something perhaps at least use one of their featured partners for real code? https://github.com/tighten/jigsaw/blob/main/src/Jigsaw.php

I also would argue that the majority of code I see in other languages is equally or worse than the example you gave.

You can write terribly in any language, Laravel included.

I think most people outside php have no idea what Laravel is, while anyone working in php is using Laravel so basically our viewpoint is Laravel = php. I don't know what it is about the normal personality that anyone using something you don't understand just be "doing it wrong"
I see php sort of like a trusty hammer. It's a hammer. Doesn't have to be elegant. It's a hammer.

The nails are http requests and PHP is a damn good hammer.

Cue the infamous double-claw hammer meme. I'd agree PHP is quite useful for web work, especially backend. And like a specialty hammer it wouldn't be useful in a lot of situations such as desktop apps or DB procedures.
Hello World in PHP is literally

    Hello World
I know no other language that elegant.
If programing stopped there we wouldn't have an argument, its when you design high performant code that is maintained for over 10 years etc.
From the top of my head, Javascript and Common Lisp will also work with "Hello World" (with quotes).
I can’t speak for Lisp, but JavaScript isn’t going to write that to standard out, it would just be a statement that didn’t do anything.

You would need something like

    import { stdout } from 'process';
    
    stdout.write("Hello World");
Fair enough. On the client (just opening dev tools) it would return the string, but as you say, it depends on what we count as valid (repl, server-side, client). Probably the closest would be

    console.log("Hello World")
Being able to do a trivial hello world doesn't make a language elegant.
The easiest Quine imaginable!
m4
This depends a little on the taste of each person, but personally the most elegant way of doing PHP apps I ever found is with Siler: https://siler.leocavalcante.dev/v/main/ and Create Siler App: https://github.com/leocavalcante/create-siler-app
PHP is elegant and useful. The spread operator is ...$array is elegant. $var ? "not found"; is really elegant.
It’s also about ten years later than everyone else to get those splat and null coalesce operators so it’s not like that elegance is unique or even timely for PHP.
Your post is 10 years too late because we are talking about php today. PHP was elegant in different ways 10 years ago.
I think you meant ?? or ?= ?