News for hackers. Stuff that matters.
Re:Modern PHP without a framework by userbinator on Saturday March 31, 2018 @10:05PM (#16726306)
I'm seriously not sure if this is satire. It starts off with a simple example, and then goes down a rabbit-hole of massive complexity to ultimately do the same thing. This reminds me of the many OOP articles that were common shortly before the turn of the century, which would take as an example a perfectly straightforward and simple-to-understand piece of code and then bloat it into an asinine multilayered monstrosity by applying a generous amount of "OOP best practices and patterns" while straight-facedly enumerating all the alleged benefits.

When you first started with PHP, you may have used includes or requires statements throughout your app to bring in functionality or configuration from other PHP files. In general, we want to avoid that because it makes it much harder for a human to follow the code path and understand where dependencies lie.

Autoloading just means that when your application needs to use a class, PHP knows where to look for it and automatically loads it when it's called for.

How in $deity's name can you say that specifying immediately the exact path to the file included, in the file that includes it, is "much harder to follow" than hiding it behind the indirection of the autoloader and whatever it does!? Honestly, WTF!?

(Maybe the joke here is that everyone is just pretending to admire the emperor's new clothes, in which case I apologise.)

Edit: here's an example of the OOP articles I mentioned: https://csis.pace.edu/~bergin/patterns/ppoop.html

Re:Modern PHP without a framework by contingencies on Sunday April 01, 2018 @02:08AM (#16726999)
Yes, unfortunately I don't think it is satire, unless they've gone to immense effort... see the guy's twitter feed, etc.

The author recommends building some kind of abstract structure factory which has perhaps five external dependencies per codeblock, a composer file, vague notions like dependency injection, auto-wiring, containers, routing, middleware, request factories, response interfaces, a class hierarchy and an obtuse non-production webserver in mental overhead ... lauding the design because he says it complies with a bunch of numbered standards that allow you to swap out your layers-of-assumption components.

This is blatant false economy. Nobody ever swaps out components. Nobody in fire-and-forget webdev land cares about standards. Just get the thing out the door, keep it simple, and thus keep it maintainable.

As others have commented, explicit code is clearer. Clearer code has less bugs.

Why not save the hassle and just use:

<html><head></head><body>Hello, <?php print anti_xss_function_of_choice($_GET['worldtype']); ?> world!</body></html>

This is the 'new PHP' from ~1995, seamlessly using decades of high efficiency improvements to kernel caches, networking gear, the interpreter itself and webservers. Zero complexity, not a class in sight, and definitely no middleware. It's a great language, when not misused.

Re:Modern PHP without a framework by lkrubner on Saturday March 31, 2018 @10:48PM (#16726423)
I get downvoted when I say anything remotely negative about PHP, in threads devoted to PHP, but perhaps my thoughts will make sense if I emphasize that I used to love PHP. It was my first real language, and I built my first business around it. That was in the year 2000. That was in the era before modern package managers, when PHP's huge standard library was a tremendous benefit, because the alternative meant using something like Perl, where you had to download every module from CPAN and cobble it together yourself. PHP was a dream compared to that.

But 18 years have passed. Every language now has very good package managers. There is an abundance of dynamic languages, most of whom are more consistent than PHP.

I'm not going to waste your time with a long PHP rant. I just hope that everyone is aware that the huge benefits that PHP offered in 2000 are no longer valid. If you are programming in PHP, you owe it to yourself to explore some of the many options that are out there. There has been incredible innovation among software languages since 2000, but PHP has mostly tended to follow a conservative path, mostly imitating Java.

For anyone who won't be bored by another PHP rant, I wrote my thoughts on this subject back in 2014:

http://www.smashcompany.com/technology/why-php-is-obsolete

Re:Modern PHP without a framework by developer2 on Sunday April 01, 2018 @02:45AM (#16727084)
Because every time someone rants against PHP, they do so in the same way you've done, saying things like "If you are programming in PHP, you owe it to yourself to explore some of the many options that are out there".

All you do is insult the language, and subsequently, the intelligence of those who continue to use it. Many of us have looked at - and heavily used - other languages, and choose to come back to PHP for many (not necessarily all) projects. Your assumption that we're all morons who program in PHP because we're too stupid to try other options is misplaced. You looked elsewhere and preferred what you found. Good for you. Some of us who looked elsewhere found that every language has its faults, and that PHP does the job just fine. Particularly since the performance improvements in later PHP 5.x versions, and finally the leaps and bounds gained in PHP 7, the language is more than good enough for most tasks.

I've done projects in C, NodeJs, Ruby, and yes, even Python. Python fanboys in particular seem to think their use of the language is an indicator of superior intellect or programming ability. The whole "I am smarter/better than PHP developers" gag is really, really, old.

Re:Modern PHP without a framework by wolco on Saturday March 31, 2018 @11:58PM (#16726630)
A lot has changed since 2014. PHP 7 is blazing fast, frameworks like laravel are putting the joy back into rapid development.
Re:Modern PHP without a framework by cutler on Sunday April 01, 2018 @01:35AM (#16726908)
For me the biggest non-starter with PHP is the abuse of doc comments and annotations as well as the sheer waste of space which has now been blessed in PSRs as the idiomatic way to write "modern" PHP. This same modern PHP often fills a full screen with only a few lines of real code. You could be forgiven for concluding that someone influential in PHP-land is trying to out-Java Java. PHP is a waste of space, literally.
Re:Modern PHP without a framework by esistgut on Sunday April 01, 2018 @02:52AM (#16727107)
We evalued PHP for a GraphQL/REST backend and we found some really good points: the design of Symfony, the ACL system, the huge productivity gain of API Platform (https://api-platform.com/). In the end we discarded the whole stack anyway because of the heavy use of docblocks annotations which made our test code hard to read and hard to debug.
Re:Modern PHP without a framework by lkrubner on Sunday April 01, 2018 @12:23AM (#16726708)
Laravel has been around since 2011. PHP 7 is slow and continues to perform badly in all reasonably done comparisons:

https://www.techempower.com/benchmarks/

But even if both of your points were true, the point is to look at what is happening in other languages and frameworks. Every day you spend learning about PHP is a day that you are not learning about the languages that are really moving the industry forward to interesting new places. Ask yourself, honestly, how many interesting new ideas will you learn from studying PHP, versus the more versatile (as in more platforms, and also the frontend) languages such as Javascript/Node, or the more innovative such as Go or Rust or Clojure or R or Julia? And I'm not even mentioning the more experimental languages, such as Shen or Lux.

Re:Modern PHP without a framework by userbinator on Sunday April 01, 2018 @12:43AM (#16726761)
Every day you spend learning about PHP is a day that you are not learning about the languages that are really moving the industry forward to interesting new places.

I continue to be baffled by this sort of "novelty-chasing" --- do I really care about "interesting new places" or "interesting new ideas"? No; and in fact I'd rather not, because it takes time and energy away from actually solving useful practical problems, besides the ones you create yourself in chasing after the latest cruft.

PHP is slow on an absolute scale, but chances are that a minimal, straightforward application written with it can perform better than something with a bloated framework on top in a different language. That takes true skill and experience, not something you can get if you're continuously jumping around trying to chase the fashion trends.

As someone who has witnessed several major trends in programming come and fall, the amount of churn (and encouragement thereof) in the web community is astoundingly unsettling.

"It's not about the tool, it's how you use it."

Re:Modern PHP without a framework by cutler on Sunday April 01, 2018 @01:39AM (#16726926)
Try writing a web app in Luminus (Clojure) or Phoenix (Elixir). I guarantee you will be enlightened. The danger, however, is that you may lose your tolerance for OOP languages thereafter.
Re:Modern PHP without a framework by conradfr on Sunday April 01, 2018 @05:45AM (#16727525)
Elixir/Phoenix is great and promising but you don't get everything you have in Symfony out of the box. For example the EEx template system is behind Twig (I especially miss inheritance) and Auth/Autorization is kind of a mess.

The ecosystem is also smaller and deployment in Elixir is still a work in progress (mismatch between Mix and Erlang releases).

Re:Modern PHP without a framework by linoge on Sunday April 01, 2018 @07:49AM (#16727801)
Yeah because inheritance was always such a great idea :D
Re:Modern PHP without a framework by conradfr on Sunday April 01, 2018 @10:12AM (#16728353)
Well as Jinga2 and Twig showed, it is for templates ;) Compared to traditional nested includes.
Re:Modern PHP without a framework by lkrubner on Sunday April 01, 2018 @01:27AM (#16726881)
Maybe you don't realize this because you haven't studied them, but "bloated framework" is unknown in Go or Clojure or Rust. Each of these languages tends towards minimal frameworks. Clojure, in particular, is known for its composeable libraries which make frameworks a bit of a sideshow for that community. Go is known for having enough in its standard library that some aspects of frameworks are not needed.

About this:

"because it takes time and energy away from actually solving useful practical problems,"

You can not know that without first studying other languages. You might find that you are 10x more productive in Rust. You can only find out by studying Rust.

But you bound the conversation in ways that I didn't when you write:

"in the web community"

I feel that developers can learn a great deal if they study projects that don't necessarily have anything to do with Web programming. Nothing in the article or on this page necessarily restricts us to only considering Web programming.

"It's not about the tool, it's how you use it."

I wouldn't use a hammer to go fishing. Nor would I use a saw to turn a screw. Nor would I use a frying pan to tune a piano. It helps to learn about a lot of different kinds of tools, because over the course of your career, you might be confronted with diverse tasks.

Re:Modern PHP without a framework by lkrubner on Sunday April 01, 2018 @08:02AM (#16727831)
Why was this downvoted? What in this comment is untrue?
Re:Modern PHP without a framework by bluesnowmonkey on Sunday April 01, 2018 @01:47AM (#16726943)
> I continue to be baffled by this sort of "novelty-chasing" --- do I really care about "interesting new places" or "interesting new ideas"?

Is this a troll? No you don't need to care. Feel free to go live off roots and tubers in the forest. Or write some COBOL. Anything is justified if you're putting "interesting new ideas" in quotes.

I worked with PHP full-time for several years and energetically rationalized away the time I invested into building that expertise. I liked it, I was getting things done, it would help me get jobs in the future. Utter nonsense. What a waste of my life.

Eventually I snapped out of it. Turns out you can just move to places Silicon Valley, there are jobs at awesome companies working with the cool modern technologies. You don't need anybody's permission, just do it. Want to write Haskell/Go/Rust/Elixir/Clojure every day? Want to work with ML or robots or big data or whatever floats your boat? Just go do it. You don't need to sit there writing PHP as the world passes you by.

PHP is a backwater. People live there and tell themselves that it's fine, good as any place really, can't imagine anything better, probably wouldn't like it anyway. Or they just stop thinking about it.

Re:Modern PHP without a framework by wolco on Sunday April 01, 2018 @09:31AM (#16728166)
PHP was never in fashion. In the beginning it wasn't as powerful as Perl. Then it was looked upon as not serious compared to java or hobbist by the business asp crowd. At one point because of a facebook movie it was in fashion for a year but that created a I hate php backlash. Php has changed so much I'm seeing people rediscover it.

Remember java was cool or mongodb or node or ruby on rails. It feels like go is becoming less cool. I wonder what next year will bring? Perhaps php will be rediscovered when SV changes once again.

Re:Modern PHP without a framework by lkrubner on Sunday April 01, 2018 @08:09AM (#16727853)
Exactly. Anyone who puts “interesting ideas” in quotes is literally a blub programmer, as Paul Graham described:

http://www.paulgraham.com/avg.html

Re:Modern PHP without a framework by exclusiv on Sunday April 01, 2018 @03:58AM (#16727270)
Some people could care less about SV. Or maybe they want to ship products their clients can handle after launch. Whether you like it or not PHP has created more value than any of those languages you mentioned. It's like you're saying it's better to be a Ferrari mechanic than a Toyota mechanic. The user does not care. So ship stuff that matters in whatever tool of choice.
Re:Modern PHP without a framework by linoge on Sunday April 01, 2018 @07:51AM (#16727804)
Then however we're on a platform made by Paul Graham which stated that lisp delivers more value than any other language.
Re:Modern PHP without a framework by gremlinsinc on Monday April 02, 2018 @12:10AM (#16732264)
DHH says the same thing about rails/ruby.

Taylor Otwell says the same thing about laravel/php.

Mark Zuckerberg says the same thing about React / HHVM / Hack (PHP variant).

I'm sure Steve Jobs said the same thing about Objective C or Swift.

It's called an opinion, just because one person who spends more time investing in VCs, and just because we're debating an issue on a site, doesn't mean we have to take the creator of the site's past essays as the absolute truth...

Go, php, ruby, python, lisp variants ALL are the best script if A. it's the only one you know and you just want to launch something or B. You've done your due dilligence to figure out what you need, is it just a crud app? PHP is fine, is it a chat app? Well you might need erlang or elixir on the backend, or something with concurrency.

If you're spending 100+ hours debating on platform instead of building shit, then that's time you can't get back and that's wasted opportunity acquiring customers to a platform you could've had built last week -- if you weren't stuck in...confusion about what framework to build in, or spending all your time in tutorials because you picked the shiniest framework or language that is above your pay-grade so you have to learn everything.

Re:Modern PHP without a framework by badestrand on Sunday April 01, 2018 @12:50AM (#16726782)
> how many interesting new ideas will you learn from studying PHP, versus [...] languages such as Javascript/Node

This is on point! Programming PHP is not about diving deep into interesting language constructs or creating beautifully structured software - PHP is about getting things done, about starting hurdle-free and progressing rapidly. It goes against some "good development practices" but it works, see for example the single-file website remoteok.io from Pieter Levels: https://twitter.com/levelsio/status/938707166508154880

Re:Modern PHP without a framework by Annatar on Sunday April 01, 2018 @03:41AM (#16727225)
Love it. Back to the roots - one file, load it up in the editor, if one needs to debug something, just search for it. Yep. This is how it should be: easy to debug.
Re:Modern PHP without a framework by Annatar on Sunday April 01, 2018 @03:35AM (#16727204)
Every day you spend learning about PHP is a day that you are not learning about the languages that are really moving the industry forward to interesting new places.

That's "chasing shiny". Shiny is the cancer destroying the computer industry from the inside.

Any day not learning how to do web application development in LISP is a day wasted. The old tech is still cutting edge, way ahead of any new shiny garbage, because that garbage is decidedly not moving the industry forward, just introducing one more variation of making a computer do something. And that's extremely bad.

Re:Modern PHP without a framework by CiPHPerCoder on Sunday April 01, 2018 @12:42AM (#16726755)
> But even if both of your points were true, the point is to look at what is happening in other languages and frameworks. Every day you spend learning about PHP is a day that you are not learning about the languages that are really moving the industry forward to interesting new places.

Which of those languages give you NaCl out of the box?

Re:Modern PHP without a framework by Lazare on Sunday April 01, 2018 @01:29AM (#16726885)
Your comments are very valid for 2014-era PHP, but I'm not sure you understand how much PHP has changed. The benefits PHP offered in 2000 are no longer valid because those are table stakes, absolutely. What about the benefits PHP offers in 2018?

> There has been incredible innovation among software languages since 2000, but PHP has mostly tended to follow a conservative path, mostly imitating Java.

PHP fans like to point out (tongue slightly in cheek) that PHP is the first major language to add modern cryptography to its standard library (libsodium in php 7.2). We can argue about how important that is, but it's also not wrong. And it certainly didn't copy that from Java. :) Further, I think some of your criticisms in the link have also not aged well.

Re:Modern PHP without a framework by wwweston on Sunday April 01, 2018 @04:05AM (#16727291)
> Every language now has very good package managers.

Not sure if composer is "very good," but it's generally worked out better for me than gem.

(Also, package managers are not an unqualified benefit. I like them for installing things I'm not developing, but as far as I can tell they essentially defer a legibility cost for developers. Sometimes worth it. Not always.)

> There is an abundance of dynamic languages, most of whom are more consistent than PHP.

I think it was Ralph Waldo Emerson who had some choice words about consistency. And this criticism has always rung a little hollow to me. I'd agree that some of PHP's dynamic multi-paradigm scripty siblings feel cleaner, but having written code in many of them, it's never felt like a major difference in productivity to sometimes have to pause and consider argument order or precise names. The lookup cost has always seem cheap on on par.

> I just hope that everyone is aware that the huge benefits that PHP offered in 2000 are no longer valid.

There's definitely a closing in the benefits gap since 2000. PHP is far from my favorite language. PHP is not right for many projects. There are still some benefits that PHP prioritizes that I think arguably aren't done better elsewhere

* Trivial deployment story. Your app is one or more files. Drop it in a directory on a wide variety of hosting environments. You're done! (Buuut, if you want something more involved, lots of choices available there too.

* The language is the template language (though there's other options). Some people consider this an anti-feature because it allows amateurs to build ill-organized applications around a dynamic-document-as-program paradigm. I think that's still a brilliant gateway that makes the marginal cost of moving from a static document to a dynamic one small while still allowing an easy transition to front controllers. And it means the templating performs at parity with the language itself and can do as much as the language can do.

Pair it with a simple routing library and model layer and it's as easy to build something reasonable quickly as it is with comparable Python & Ruby frameworks, often with fewer moving parts and similar magnitude of performance.

So sometimes when I have smallish web-facing projects (or projects I think I might want to distribute for others to run on the web), I still often find myself reaching for it, even with plenty of other options at hand, even with languages I like better.

Re:Modern PHP without a framework by sethrin on Sunday April 01, 2018 @01:41AM (#16726931)
Autoloading allows one to specify modules by their logical, namespaced identifiers rather than simply importing a file. One benefit is that one can swap out the implementation without necessarily changing the code. I've had to override some badly-behaved libraries that way.

Generally speaking, while I don't think this is satire, I would hate to see someone code this way, unless the point was simply to learn how the components of a web framework work together. This is not "framework-less PHP", it's just a DIY framework, and not a particularly good one. I think that attempting to create a web application without a framework is at best needlessly duplicative of effort, and at worst an open invitation to security flaws and spaghetti code.

The author says, "This is not an anti-framework screed," and it's certainly not polemical. However, I think it's probably more representative of the Zend-ecosystem way of doing things rather than PHP best practices: accretion, as opposed to design.

Re:Modern PHP without a framework by slim on Saturday March 31, 2018 @11:06PM (#16726479)
Some time ago there a guy that posted a "Show hn" showing his "lightweight templating engine in php". He parsed a HTML file and replaced <% %> tags with some text. It never occurred to him that php is actually a templating engine
Re:Modern PHP without a framework by scintill76 on Saturday March 31, 2018 @11:16PM (#16726516)
To be fair, if the <% %> tags HTML-escape the text, then it might be considered better than raw PHP.
Re:Modern PHP without a framework by slindsey on Saturday March 31, 2018 @10:22PM (#16726365)
Thank you. You stated what I was thinking much more clearly than I was able. I strongly prefer explicit code that states clearly what is happening without having to guess.
Re:Modern PHP without a framework by lotyrin on Saturday March 31, 2018 @10:41PM (#16726406)
No, this is probably serious; this is The New PHP.

Seems we finally broke the fever of infectious imperative spaghetti and are coming around to OOP and patterns by following the trail left by JVM ecosystem and eating its droppings and clothing ourselves in its shed skin.

Re:Modern PHP without a framework by ehnto on Saturday March 31, 2018 @11:29PM (#16726558)
Huh. Now I can't tell if this one is satire as well.

You are on the mark for Java like OOP becoming popular in PHP. I think there are more people trying to do enterprise level work with PHP and they are taking lessons from existing software engineering practicies.

Re:Modern PHP without a framework by TylerE on Saturday March 31, 2018 @11:41PM (#16726588)
It's a sort of reverse natural selection. The people writing PHP these days for whom the last, oh, 15 years or so weren't enough to stop writing PHP.
Re:Modern PHP without a framework by ehnto on Sunday April 01, 2018 @10:55AM (#16728582)
What I found interesting was a period around 2008 where PHP still had "easy deployment" as a pro in its belt. Some enterprise level projects looking for easy adoption chose PHP and tried to build as they would have in Java.

One example is Magento, and arguably it was the right choice at the time and has given it enduring popularity (if not endearment) for nearly a decade.

As more software engineers took web applications seriously they brought best practices and tech stacks with them, and you can see the PHP community really strive to level up.

But if I were to make a bold statement, it is web development in general that struggles producing solid reusable work. You see it in all languages. The only ecosystem I can think of that isn't disparate islands of unique wheel factories is Ruby, and that seems to be because people learn "rails" not ruby.

Re:Modern PHP without a framework by NightlyDev on Saturday March 31, 2018 @10:34PM (#16726390)
It's not that requires are hard to follow, it's just that you most likely don't want to mess around with requiring files manually instead of easily doing it by using autoloaders.

You can do it, but it's most likely a bad idea to mess around with it considering namespaces and such.

Re:Modern PHP without a framework by reificator on Saturday March 31, 2018 @10:15PM (#16726347)
Even if everyone is in on the joke, it's good to have comments like this explaining to newcomers that this is not how things should work.

Disclaimer: I've not written much php nor read the article. Just replying to this one comment because the OOP analogy spoke to me.

Re:Modern PHP without a framework by mfonda on Saturday March 31, 2018 @11:04PM (#16726474)
Autoloading is pretty simple. PHP knows exactly where to look for code, and you know exactly where to look for it too. Class names are mapped exactly to file names [1], so autoloading lets you simply use a class without needing to explicitly include the file.

There's a number of issues inherent in manual include statements:

- When you include a file, unless you include it using the absolute path, the path will be resolved using the include_path ini setting, which works just like the $PATH environment variable works to resolve the location of binaries. Typically, `include_path` will be set to something like `/usr/share/php:.:/some/other/dir". If I encounter `require_once 'Foo/Bar.php';` in some code, what file will that include? `/usr/share/php/Foo/Bar.php`, but maybe also `./Foo/Bar.php`.

- Relative includes are relative to the current working directory, so a different file may be including (or not found at all) depending on what directory I'm executing my script from. To avoid this, it's good practice to include files using `include __DIR__ . '/Foo.php';` to ensure we know exactly what file is included.

- Prior to autoloading, most dependencies were installed in a global location, e.g. `/usr/share/php`, which would then be part of include_path. Different projects on the same machine therefore had to have the exact same dependencies. Autoloading enables per-project dependencies.

- Using manual includes makes it much easier to have a poorly structured project. With autoloading, if I see `$bar = new Foo\Bar();`, then I know exactly what I'm getting: the class Bar, defined in the file `src/Foo/Bar.php;`. On the other hand, if I see `include 'bar.php';`, I have no idea what I'm getting. What's in the file? Could be class, could be bunch of random functions, could be mish mash of HTML and PHP spaghetti.

Same of these issues are manageable even without autoloading by following best practices, but unfortunately there's a lot of really bad code out there that doesn't. Autoloading (along with composer) completely solves a problem that used to exist in PHP. Anyone that actually uses PHP can attest to this. If you've ever worked on a project with a web of manual includes, it's a total nightmare compared to a properly structured project using autoloading.

Autoloading aside, the point of this article, IMO, is that it's quite easy to set up a well structured, maintainable, easy to understand project architecture without using a major framework. The central abstraction here is that all requests go to a front-controller, which then calls a function that accepts a representation of the HTTP request and returns a representation of the response. Having abstractions for Request and Response is much more manageable (and testable!) than having random print statements everywhere and using global variables like $_POST. This is same idea behind just about all web programming "frameworks" in any language. Of course this would all be over kill if all you needed was a "Hello World" page. But this isn't about hello world pages, it's about real projects that grow over time and need manageable structure.

Furthermore, this article advocates for dependency injection. Again, overkill on a hello world page, but in a real project explicit dependencies makes life so much easier.

[1] This is the de-facto standard. Sure you could technically write an autoloader that does weird things, but nobody--human or automated tools--does this in real code. It's not a problem in practice--autoloading in the real world makes things much more explicit than manual includes.

Re:Modern PHP without a framework by liveoneggs on Sunday April 01, 2018 @07:05AM (#16727699)
wow I logged in just to call out this same quote!

I'd also like to encourage everyone to strace a php run sometime and then tell me how great autoload is..

    stat(path1/Class.php)\ Not found
    stat(path2/Class.php)\ Not found
    stat(path3/Class.php)\ Not found
    stat(path4/Class.php)\ Not found
    stat(path5/Class.php)\ found!
    open(path5/Class.php)
    read(path5/Class.php)
Re:Modern PHP without a framework by Annatar on Sunday April 01, 2018 @03:29AM (#16727184)
What seriously bugs me is that he uses object oriented programming as if it were the most natural, normal thing. Debugging an object oriented program is a nightmare because all the data and therefore the machine state is hidden, that's the idea.

If I were to write a website in PHP, I'd use functional programming and certainly no framework. And I'm still scarred from the Autoloader in Perl. Never again!