

Going Back to PHP - chasingsparks
http://pathdependent.com/2009/10/26/going-back-to-php/

======
jrockway
Do serious PHP developers use PHP's built-in template language? Yahoo and
Facebook don't.

PHP is also not tuned to some fitness function; it tunes its users. In
exchange for not having to understand abstraction or OO, you get to learn the
wierd quirks of thousands of inconsistently-named functions that live in a
shared global environment. Sure, the number of "concepts" you have to know is
low (no closures, no MOP, no lexical scope, ...), but the number of inane
details you have to know is high. PHP shows that people would rather
understand the exact details of arguments to a function instead of
understanding abstraction and metaprogramming. (Similarly, people like to cram
for exams without really understand the subject matter.)

That is fine, but unless you are "people in general", you are missing out on a
lot by "choosing" PHP.

(Deployment is always a big issue. Do you really want to market your app to
people that can't even run a package manager, "cpan" or equivalent, and so on?
I find Perl perfectly easy to deploy. If I can't "apt-get install" the app, I
type "cpan AppName", wait a while, and my app works.

Additionally, users that find deployment too hard unless it's "untar this and
click pictures" are going to require a lot of support but aren't going to
contribute a lot of code back to you. So maybe this isn't the best
optimization to make.

Finally, why not JSP for this? Startup Tomcat, edit code in a web page, and
boom, your "app" works. It ain't pretty, but it's better than PHP. Tomcat is
no harder to setup than Apache + mod_php either.)

~~~
wvenable
> Do serious PHP developers use PHP's built-in template language?

No. However, my template engine compiles to PHP's built-in template language.

> In exchange for not having to understand abstraction or OO

My PHP code is just as object-oriented as my C++, Java, or C# code. As for the
functions, you memorize what you need and have a good IDE for the rest. One of
the benefits of PHP is that there's a function for just about every common
task and using them is really easy. Other languages may have the same
functionality in a library, but the setup and calling is far more complicated.

PHP is a get-things-done language and it always has been. You can dick around
with all the abstractions, frameworks, and meta-programming you want but often
that's just more complicated and a waste of time.

~~~
mbreese
> my template engine compiles to PHP's built-in template language

No, PHP _is_ a template language... just a very complete one.

> PHP is a get-things-done language and it always has been

No, PHP is more of a spaghetti language. Sure, it's easy to get something
working. Make a tweak here, a bug fix there. If you need a quick knock off,
just copy the files, copy and paste a bit, and you're good to go.

But after going through that a few dozen times, you quickly end up with a
tangled mess of spaghetti code. Speaking as someone who wrote and had to fix
plenty of code (from myself and others) like that, it's not something that I'd
like to do. PHP makes it very easy to get trapped like this.

Like any language, it is possible to write good, well abstracted code. Unlike
others though, PHP makes is very easy to write bad code. I don't know why...
perhaps it's that the learning curve is _too_ low.

The newer versions are definitely trying to elevate the language, but I think
that ship has already sailed.

~~~
wvenable
> If you need a quick knock off, just copy the files, copy and paste a bit,
> and you're good to go.

That's a style of programming, it really has nothing to do with the language.
I can cut'n'paste code in any language and I can write nicely abstracted
modular code in PHP. Why are you confusing the two?

> PHP makes it very easy to get trapped like this.

Really? Why is that? What is _so_ different about PHP than every other
language in existence? You don't even know, do you?

> Unlike others though, PHP makes is very easy to write bad code. I don't know
> why... perhaps it's that the learning curve is _too_ low.

I'm not sure how the learning curve would have anything to do with it. If you
already know how to program well, PHP isn't going to break your legs. You're
not suddenly going to stop doing good design because of a few dollar signs,
are you?

~~~
jrockway
The dollar signs are not the problem with PHP's variables. Their global scope
is.

But anyway, well-abstracted PHP ends up looking like Java -- very verbose.
Quick example; if you want to pass around a callback that has state, you have
to make it a class, since there are no closures (instances of classes and
closures are about equivalent). This is, however, a lot of typing to do
something simple. Instead of a quick one-liner, you now have a new file and a
new class, or, at the very least, an interface and anonymous class. Messy.

You can't abstract away that pattern, either, as there is no MOP (to auto-
generate the classes as needed), or macros (to at least type in the code for
you automatically).

Anyway, something that's just a few characters in other languages is now
multiple files. Sure, it's well-abstracted and pretty easy to understand. But
it's also massively over-engineered, and distracts from the task that you were
actually trying to accomplish.

If you are going to write PHP code that looks exactly like Java, why not just
use Java and get your 70x speed boost?

~~~
wvenable
> The dollar signs are not the problem with PHP's variables. Their global
> scope is.

I think you sort of have that backwards, all variables in PHP are local unless
you go out of your way to make them global (or just start typing into an empty
file without any functions). I pretty much surrender the global scope. None of
my variables are global except for static class members. I don't have any
global functions, except static class methods.

> Quick example; if you want to pass around a callback that has state

I don't pass around callbacks that have state very often. I tune my
programming style to the language that I'm working on. Often there are
different ways of handling the situation in PHP -- it's is actually quite
dynamic in places. I prefer to keep my pages pretty KISS anyway -- I can do
hell of a lot (see my other replies) without needing stateful callbacks.

> If you are going to write PHP code that looks exactly like Java, why not
> just use Java and get your 70x speed boost?

I don't know, why are Java sites so painfully slow and all PHP sites so fast?

~~~
jrockway
Because the Java sites are solving actual problems, and the PHP sites are
selecting one row from a database and adding <blink> tags around it?

~~~
wvenable
Niice. So much for intelligent commentary.

------
neilk
PHP has a lot of virtues. It's fast. It has libraries for almost everything a
web programmer needs. Deployment is trivial with SCP and with rsync scales to
hundreds of machines easily, with barely more infrastructure than a shell
script.

Even its supposed deficits are advantages sometimes. PHP stops you from a lot
of temptations to go non-REST. Anyone who's seen a Java project where the web
server is spawning multiple threads per request can understand.

It just gets incredibly annoying when you try to do anything complex with
programming logic. Due to its rather low ceiling of abstraction, PHP
encourages you to cut and paste rather than refactor. Again, not actually a
problem for many, until you get to a very advanced stage (say three years) and
how many projects even last that long?

~~~
adrinavarro
PHP is very good for little, dirty projects coded in a weekend, but also for
big, long-running projects (well, maybe Java and Rails are better for that).

Facebook, Yahoo! and lately (at least, looks like) twitter (that's just a
guess after seeing their server-status pages) do use PHP. I do, also.

You can build huge projects using a self-made (or external maintained)
abstraction structure fitting your needs (database, data clustering,
middleware). Or just a countdown page with the days before the next party (?).
And, in both examples, it wouldn't be slower to code things with PHP than with
RoR.

~~~
aaronblohowiak
Using PHP does not mean that it is turtles all the way down. Best tool for the
job, i suppose.

------
strlen
PHP is one language, I am _absolutely_ not willing to (primarily) develop in.
I've left a PHP-only job almost entirely over the matter of language choice
(there PHP was used for _everything_. My worst decision there was -- after
getting a Hadoop cluster going -- showing the team how to do Map/Reduce in PHP
via the streaming API).

I've never felt a strong repulsion to any language like I do with PHP and I
wonder why. I think at the core of are these facts: it's optimized for _web
development_ , doing anything complex or CS/math intensive (developing
algorithms, doing off line computation, systems programming) is sometimes
possible, but always painful. I've played with PHP3 before college and other
than what I've learned in a databases course, very little what I've learned
_in a university_ seems to be applicable to routine PHP programming. It
_doesn't feel like actual development_.

Ironically, PHP seems to work very well in conjunction with more powerful
languages: Yahoo, Facebook and others are built with PHP as essentially a
template layer, with Java/C++/Erlang/Perl/Python on the backend. Perhaps the
fact that coding PHP doesn't feel like software engineering _is_ it's greatest
asset. It's a language that a _web developer_ can immediately be productive
in. Yet it also exposes what is often tediousness and triviality of most web
development. I remember first playing with emacs, SLIME, cl-sql, mod_lisp and
UCW: while I was writing a web application (something PHP is well suited for),
I felt I was excercising my brain a great deal more. To a lesser extent it
felt true with Python/CherryPy and Perl/mod_perl.

Perhaps what makes PHP successful at Yahoo and Facebook is that they're
serious, well-funded technology companies who are able to attract talented
developers (who can take advantage of PHP's easy interoperability with C). An
_early stage_ start-up choosing PHP seems to almost doom itself by attracting
a crowd that treats PHP as Blub (rather than as a sometimes appropriate tool),
or in less refined terms "script kiddies".

------
petercooper
_Conversely, PHP has one several advantages over Rails. Most notably,
immediacy. Deployment via SCP is very satisfying._

Though not as satisfying as using source control and having a one command
"deploy" that's just as quick as SCPing a bunch of files.. I can deploy - to
production - local builds of my Ruby projects in seconds.

There are no significant advantages to using PHP or disadvantages to using
Ruby or Rails presented in that blog post, even though it tries to make it
seem that way. (And on the library complaint, the Ruby libraries that came
along for things like S3 and EC2 are better than the official ones by a long
shot.)

Despite that, different strokes for different folks. Use what works for you
and all that!

~~~
steveklabnik
I come off like a Capistrano fanboy in this thread, but I guess that I kind of
am.

I only ever used the SCP method when I did PHP stuff, but the projects were
never anything that big. Now that I do Rails all the time, having a tool like
Capistrano is amazing. 'cap deploy' copies my code, changes the symlink,
updates my database schema, notifies my error reporting, restarts my
webserver, and cooks me dinner.

Okay, I exaggerate. But seriously. Automation! It's awesome.

~~~
petercooper
It is! And even if you just jury rig it! Several of my projects have a deploy
script that's just a bash one liner that does a _git push_ and does a _git
pull_ and touches a restart file on the server :-)

~~~
steveklabnik
Another advantage of this is that it makes you actually commit to your
repository, if you're bad at that kind of thing.

------
tptacek
You could have deployed code using SCP with Passenger, which is basically Ruby
deployed PHP-style and is basically the emerging standard for how Rails apps
are deployed. So immediacy seems like a weird reason to take such a step back
on languages.

But I mean, do whatever makes sense for you. PHP apps are good for business
for us. =)

~~~
ionfish
Or better yet, via git push and a post-receive hook to touch
tmp/restart.txt...

~~~
steveklabnik
Or better yet, using Capistrano to do that and more.

~~~
tptacek
Things like Capistrano are the reason this person gave up Rails for PHP, and I
don't really blame them.

~~~
steveklabnik
Why? I mean, for a very basic setup, you just run 'capify .', and then change
some passwords, and you're good to go. It doesn't even really take any
configuring.

Here's the thing: good engineering means that you should automate as much as
possible. That way, everything is consitant. One command deployment is
amazing, regardless of your technology stack.

~~~
tptacek
It's more complicated than just scp'ing files over. That's all I have to say.
It's fine to say that it's also better engineering, but that doesn't address
the original article. Ruby is also a better engineered language than PHP in a
lot of ways.

~~~
dasil003
_Ruby is also a better engineered language than PHP in a lot of ways._

I would say better _designed_ , but the engineering definitely leaves a lot to
be desired. Here's hoping Rubinius gets finished one day.

~~~
ionfish
Well, there's also JRuby, and YARV is a lot better than MRI, although the
libraries haven't quite caught up yet.

------
chasingsparks
I got 9 up votes for my comment on PHP in
<http://news.ycombinator.com/item?id=903402> so I wrote this blog entry while
at work. I have a feeling the post makes me sound like a douche. We'll see.

~~~
nir
Being concerned that "the post makes me sound like a douche" by definition
places you outside the douche set.

~~~
alttab
Truth, and truth.

------
tybris
Using PHP hits back at you after a few years when you find out all its
libraries are slowly rotting, revealing fundamental security flaws that will
give you unimaginable headaches. These days I select my languages purely by
programmer competence.

~~~
jrockway
So true. PHP library implementers seem to love making everything a strange
corner case instead of taking a step back and picking a better abstraction. It
is amazing how easy it is to write code that works for all possible inputs
when you pick the right abstraction, and it's amazing how downright difficult
it is when you pick the wrong one. PHP libraries function accordingly.

------
neovive
After reading up on the powerful SPL classes included with PHP5 and developing
on top of the very well-coded KohanaPHP framework, PHP development has become
very productive lately. Although, I prefer Python's syntax, the deployment
speed of PHP is very hard to match.

The inconsistent function library definitely gets annoying, but I guess that's
why PHP has such excellent documentation. Using Eclipse PDT code hinting also
mitigates the API annoyances.

------
adriand
When I was first learning Ruby on Rails as a proficient PHP programmer, the
company I worked for landed a largish (for us at the time, given there were
only two programmers in the place back then) contract for a web application.
Since I was just learning Rails, and I needed to be highly productive on the
project, I recommended that we use CakePHP so I could use my PHP skills. I
figured my co-programmer, who loved Rails, would acquiesce due to the presence
(at least) of an MVC framework, and I was right.

I've regretted that decision many times. Not all MVC frameworks are created
equal, and I can say from painful experience that Rails is far superior to
CakePHP. We got the job done, and I may have been right that I was more
productive in PHP back then than I was in Ruby. But three years later, it is
far more difficult to get things done on this web app than if it were built on
Rails.

I'm not one to engage in language wars, and I still enjoy coding in PHP. But
if you want a great framework, I'm still not sure you can beat Rails, and I
really don't think there's anything in the PHP world that can.

~~~
trevelyan
Just out of curiosity, what problems did you run into specifically? I adopted
CakePHP for similar reasons and haven't run into any issues that have made me
regret it, although I did leave standard development practice for a few
reasons that have made caching a bit more difficult than otherwise.

~~~
adriand
Sorry, I tend not to notice when people respond to my comments, it'd be great
if this site had an easier way of letting you know when people do (maybe I'm
just not aware of it?)

Anyway, the main issues I ran into were all related to database queries. This
application was not a standard CRUD-style app at all, it has a lot of pretty
complex queries for building reports. I found the way that CakePHP deals with
joins and associations really clunky and hard to use compared to Rails'
ActiveRecord. I'd often have to write the raw queries and try to shoehorn them
into CakePHP's ORM objects.

I just found myself frequently frustrated with the way it was working. It's
been too long since I really worked on it for me to be able to give much more
detail than that (also, it probably has improved a lot since then too).

That said, using it as a web framework still made coding the app a heck of a
lot easier than it would have been without it. Also, I found that it really
improved my PHP skills, I picked up a ton of good stuff from using it.

------
phaylon
In general I can't disagree. If it makes sense, use it. What always struck me
as odd though is the heavy focus on application code deployment.

I mostly work on larger projects, so application updates are usually simply a
pull from the repository. It is usually a bigger hassle to get a smooth
transition of the database schema, file-system storage or anything else that
holds data.

~~~
chasingsparks
That may be true. I have not seen what Cake offers in that respect.

For the few rails projects I developed, I used capistrano but for small
projects this felt unrewarding. Tangentially (to git pulls), I did notice that
PHP developers favor SVN to git, which I don't like as much.

~~~
vital101
"I did notice that PHP developers favor SVN to git..."

I can attest to this. I spend a lot of time working with PHP with a lot of
different people, and almost every single one of them uses SVN for deployment.
I'm not sure if this good practice or not, but it's just so convenient.

------
jmtame
i can only hope this post doesn't degrade into a troll war about why ruby is
better than php or vice versa... i've used both, they both work, they're both
fine. can't we leave it at that? =]

------
Zarathu
Eh. I decided to read the post because of how outrageous the title seemed, but
I can't say I really agree with the gist of what you're saying.

I don't really understand why you'd prefer to deploy over SCP, either. You
know what's really trivial? "cap deploy"

As far as speed goes, I think of Ruby as some sort of investment, in that I
believe Ruby's 2.0 era will surpass PHP, and perhaps come close to Python.

~~~
igouy
> As far as speed goes...

But will any of them catch up to JavaScript :-)

------
antirez
I use Ruby and I love this language, truly. I wrote my framework, play with
Sinatra, and so on, like you. But we are in hacker news and are in some way a
closed ring of people a bit smarter than the average programmer.

Then there is the mass of programmers that could witch from PHP to something
better. All they want is a mod_please_make_ruby_simple that is, like mod_php
but for Ruby. You install it on Apache, and all the pages called .rhtml will
get executed with the trick of things outside <? ?> being translated into
echos.

This module should also provide, automagically and built-in, support for all
the obvious stuff like file uploads, MySQL bindings and so on.

I understand that PHP got this right for the masses, but can't understand why
no one is trying to do it the right way for other much more powerful and
elegant dynamic languages.

~~~
jrockway
Because those of us that know how to implement that sort of system have no
need for it. There are thousands of things I want to implement; making
something I'll never use is not one of those.

------
techiferous
Rails + Heroku is deployment heaven. It's even easier than scp.

~~~
jshen
and expensive

~~~
techiferous
Actually, this is one thing that I really love about Heroku. They will host
apps for free. I put my <http://2rgb.com> there and am not paying a cent.

For a serious app, I'll have to start paying, but the prices seem worth it to
me because they're saving me a lot of time. Maybe it's expensive compared to
other hosts; that I don't know.

~~~
jshen
the free account only allows 5MB of storage. I can't do much of anything with
that. From there it gets expensive fast if you run multiple sites. It might be
a different equation for a single site.

------
richz
Could someone please provide a link as to how Facebook and/or Yahoo use PHP
with "templates"?

I want to create "yet another" social networking site for a niche market, and
am trying to figure out what technology would work best for this space.

I've been using asp.net for the last several years, and used to code in
mod_perl, java, etc. But, I don't have any experience with the newer open
source languages. I want to pick what's a good fit (even if I have to learn a
new language). I know these are just basic details, but would greatly
appreciate any feedback.

Thanks! (long-time lurker, first-timer poster)

------
chasingsparks
This post is meta-interesting for me. I did _not_ expect it to elicit 4,000+
unique visits. I almost want to put an: _Update: PHP S%cks!_ at the top of the
blog post just for fun (I wont).

~~~
jrockway
Your post appeals to two audiences; the blub/PHP programmer that reads your
post to justify his own tastes, and the OMG Ruby Is So Awesome programmer, who
wants to read your post to tell you how dumb you are.

I wrote about Java once on my blog. Never again. _That_ is a crazy language
community.

~~~
chasingsparks
Sadly, I think your summation is accurate.

