4 ms·
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.
by krisrm 5y 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.
- rhengles 5y agoThis 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/ https://siler.leocavalcante.dev/v/main/ and Create Siler App: https://github.com/leocavalcante/create-siler-app https://github.com/leocavalcante/create-siler-app
- KerryJones 5y agoHave 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.
- path411 5y agoI 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"
- sergiosgc 5y agoI 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 https://laravelexamples.com/example/koel/events-listeners
- KerryJones 5y agoIt'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 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 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.
- Lhiw 5y agoElegant PHP is an API with each endpoint ending in .php and query Params that look like ?hello=world. I don't know why pretty urls or magical regex routing matter so much to people but to me it's a huge waste of time when PHP and your webserver already do all that you need. The result is an API with extremely obvious and easy to work with code, every change to endpoint code is unable to blow up the whole site, library code is still a risk but things are far more isolated.
- ipaddr 5y agoPHP is elegant and useful. The spread operator is ...$array is elegant. $var ? "not found"; is really elegant.
- eloisius 5y agoIt’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.
- ipaddr 5y agoYour post is 10 years too late because we are talking about php today. PHP was elegant in different ways 10 years ago.
- keyle 5y agoI think you meant ?? or ?= ?
- donatj 5y agoHello World in PHP is literally Hello World I know no other language that elegant.
- mejutoco 5y agoFrom the top of my head, Javascript and Common Lisp will also work with "Hello World" (with quotes).
- donatj 5y agoI 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");
- mejutoco 5y agoFair 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")
- richrichardsson 5y agoThe easiest Quine imaginable!
- chippiewill 5y agoBeing able to do a trivial hello world doesn't make a language elegant.
- rei_ayanami 5y agoIf programing stopped there we wouldn't have an argument, its when you design high performant code that is maintained for over 10 years etc.
- Karellen 5y agom4
- 0x0nyandesu 5y agoI 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.
- paulryanrogers 5y agoCue 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.