
High Quality PHP - stedaniels
https://www.acquia.com/blog/high-quality-php-benjamin-eberlei
======
programminggeek
It is really easy to talk smack about PHP because as a language it is not the
prettiest, best designed, or cleanest language out there. It often leads to
some very gross code.

However, programmers write ugly code in every language and even in a much more
beautiful language like Ruby, there are so many projects that end up with ugly
codebases, poor performance, etc. that PHP by no means holds an exclusive
ownership of terrible coding practices.

Writing high quality code in any language takes significant effort on an
ongoing basis. Practices like TDD, code review, pairing, etc. can help, but
only if they are applied with significant effort.

~~~
maw
You can trot out the trope about how you can write good code or bad code in
any language as many times as you wish but it will not change the fact that
php makes it unnecessarily difficult to write good code and, through both the
language's and its standard library's misfeatures as well as the surrounding
culture of sloppiness, encourages writing bad code.

~~~
cookiecaper
blindhippo: you are hellbanned. Most readers cannot see your comments.

~~~
nobleach
As a casual HN reader and as one who doesn't post comments that often, I have
to say, that shit exists here??? my god, so we're basically reddit? So do we
have a hivemind too?

This isn't directed at you. I just didn't realize HN was part of that nerd
subculture. I thought HN was for adults.

------
beberlei
Author here. I wrote this article as a guestblogger for Acquia, which is the
company behind Drupal. I don't work at Acquia.

The article obviously uses Drupal as an example, PHPBB and Wordpress as
comparison because they are all in the off-the-shelf, customizable category of
PHP applications.

Both Drupal and phpBB (contrast to Wordpress) have massively modernized their
code bases in the last years and improved the quality of their code. Coming
from their original level, this is obviously a huge undertaking and still far
away from being "high quality code" as an absolute measure. However their
efforts in this space are remarkable.

Given that they have so huge installed code-basis that they have to take
backwards compatibility seriously (which is also something that contributes to
quality of software), explaining why it takes so long to improve the quality
of truly old legacy software.

Compare this with many companies that use PHP, that still don't know what
automated testing and quality assurance is. The article tries to highlight how
PHP open source software use tools for quality assurance, what tools exist and
that you should use them in your PHP projects.

~~~
WiseWeasel
FYI, there's currently a typo in the first paragraph ('loosing' => 'losing',)
which made me cringe; you have to bury the typos further down where they'll
have less influence on my perception of the piece.

[edit]It's since been fixed. That was an impressively quick response.[/edit]

~~~
beberlei
someone else mentioned it on twitter before, thanks for the hint :)

------
polysics
I left PHP about 3 years ago, and I have recently had to look into it again
due to a new course I am preparing to teach. The PHP I knew is no more, and it
is leaps and bounds ahead of what it used to be. But using Drupal as an
example is really a bad choice. Talk about Laravel, Composer, new language
features, but not crappy giant codebases.

~~~
girvo
It's great isn't it? Variadic functions is about to land, as is splats, and
some other neat features like "use function", which will mean a simple module
setup with namespacing without classes. Oh and phpdbg built in!

I went to leave PHP this year, and didn't end up doing it, because of how much
has changed lately. It's a good language, with awesome tooling know. I can
write well-tested, easily readable, maintainable code using small composable
libraries, and big frameworks when needed, without fighting against the dumb
parts of the language. It makes me happy :)

~~~
venomsnake
the main problem with that is that most of the big php deployments are moving
at their own pace. So ... anything new can trickle down in years. And you
rarely have the luck of finding clean slate php project nowadays.

------
jiggy2011
The big difference that the PHP ecosystem seems to have vs the ruby/python
ecosystems is an emphasis on providing complete stand-alone solutions to
problems vs providing developers with the most flexible and powerful tooling
to develop their own solutions.

Whenever I find myself wanting a photo-gallery, an eCommerce platform or a
forum I always seem to find that the most mature solutions are based on PHP.

The advantage of this is that semi-technical webmasters can easily deploy
complete websites without writing a line of code, thus the underlying code
quality is of secondary importance to the functionality provided.

~~~
polysics
That is not necessarily an advantage, considering the same semi-technical
people leave their websites available for any kind of security breach. This is
not 2001 any more, people should know what they are doing.

~~~
jiggy2011
it is what it is, if they weren't using PHP they would be using something
else. Having said that it shouldn't really be an expert level activity putting
a few webpages online.

------
nkuttler
I'm speechless that "phpBB, Drupal, Wordpress" are supposed to be the "great"
examples of "high quality PHP".

~~~
endijs
It's not what is said there. Read again: "The focus of PHP has always been on
"getting things done" and the language’s developers still value this goal
highly. This practicality has spawned great open-source software such as
phpBB, Drupal, Wordpress and various other well known applications that are
hosted on millions of servers worldwide" So - "getting things done" have
spawned great tools like phpBB, Drupal, Wordpress. They are great, because
they address problem for end users and are used by millions. It's not said
that they are high quality PHP projects if we look at code level.

~~~
nkuttler
The author loses a lot of credibility by calling those three apps "great". If
the article is about high quality PHP I'm interested in that, and not the
poster children for PHP's popularity. As a user, phpBB is a nightmare to use,
as a developer, WordPress is spaghetti code at it's worst.

Edit: What's even worse is that we don't get examples of PHP code that was
actually written with proper development procedures from the ground up. I
mean, I was genuinely hooked by the headline..

~~~
debacle
WordPress is great software, even if it isn't written well - from the end
user's point of view, it is exceptional.

~~~
berkes
> from the end user's point of view, it is exceptional.

I beg to differ. Disclaimer: I am an ex-drupal-developer, now-ruby-developer
working at a startup for managed WordPress hosting.

Many people use WordPress because they know of nothing else. They don't find
it good, exceptional or bad: they find nothing of it: it is all they know.
They can't compare.

Again many people have severe issues with WordPress, ranging from security-
holes, infected code, poorly maintained plugins to, most often, poorly
implemented themes and horrible performance.

Then, some, really like it. But those are a minority. I've heard many
WordPress-developer and -user utter the same words Dries (after Boris Mann)
wrt Drupal has uttered: "This CMS sucks least".

You'll have to dig deep in order to find developers, users and other
stakeholders, with the ability to compare it to many other alternatives, and
who find it "exceptional". I've yet to meat one; eventhough all our customers
are WordPress users.

~~~
scarecrowbob
Hey, I'm a guy who has done projects in several other PHP frameworks (Joomal,
Drupal, Zend/Magento/aMember, cake, EE, CI) as well as some work in
python/django and who tries, each quarter or so, to at least play with some
other languages and frameworks (I've done the rails book tutorials, and
tutorials with several of the JS MV-ish frameworks).

If you're working with one of the larger WP-oriented managed hosts, we have
the same clients and I have likely worked with your hosting.

So, well, here I am, nice to meet you :).

I agree... most people doing this kind of work don't have a lot of other
options to choose from-- it is just what they know. They certainly neither
know nor care to learn some other framework. Many of the people in this line
of work are selling low or mid end projects to smallish businesses premised on
leveraging functionality built by the large WP community.

That said, even though I could do projects in other systems:

I am usually working with agencies/consultancies who _want_ WP because,

it is super easy to find folks who can work with WP on some level,

their clients are familiar with WP,

and it is trivial to meet a bunch of marketing or CMS goals with it.

So while WP doesn't "suck the least" (I know of stuff I like better), it's
still a good tool for a lot of reasons that have little to do with the code.
For marketing objectives, you're going to have a hard time finding something
objectively better for the total cost of ownership (there are, of course,
better eCommerce, community forum, and application systems).

And your position self-selects for a particular kind of client... you're
probably not seeing the folks who are doing the $10K-100K projects I see, in
which the server management isn't farmed out to a hosting company.

------
thomasfedb
Of course you can write crappy code in any languages, and conversely you can
write nice code in most languages (perhaps not in INTERCAL or Whitespace).

Personally I prefer Ruby over PHP for precisely the same reason I don't begin
my day by shooting myself in the foot.

------
8ig8
For context, Acquia[1] was founded by Dries Buytaert, the founder and lead dev
of Drupal.

[1] [http://en.wikipedia.org/wiki/Acquia](http://en.wikipedia.org/wiki/Acquia)

~~~
justinph
The irony is not lost on me.

------
mekoka
It still baffles me that whenever an article like this comes up, people still
justify choosing PHP because of _its ease of setup compared to other
languages_. Seriously? What are you going to tell me next, that you chose Ruby
because the "Hello World" tutorial was 15 minutes shorter, than the Java one?
Come on now.

Yes, the fact that PHP is easy to setup may _explain_ its popularity, it
doesn't however _justify_ choosing it, considering that you may spend 2 or 3
days setting up a tool that you'll end up using day in and day out for the
next couple of years, to do much more difficult things than just greeting the
world on a blank web page.

~~~
enimodas
Lots of hate for php in this thread, but this has me thinking: does anyone
know a better solution to solve my problem? I have to build a CRUD-like webapp
for some database, and with a bunch of authentication and authorization. Lots
of forms and reports, think MS access. I was planning to use symfony2 and a
generator bundle for this, as symfony2 has those things build in and the
generator will write a lot of the code i need. I'm also pretty time
constrained (2 weeks), but I'm still curious what other options there are out
there or how other people would solve this.

~~~
Iftheshoefits
That depends on your skills. The time constraint you provided leaves little
room for other solutions you aren't already familiar with.

That said, for your use case (since you're thinking of symfony2, it's clear
that performance isn't an issue/concern:
[http://www.techempower.com/benchmarks/#section=data-r8&hw=i7...](http://www.techempower.com/benchmarks/#section=data-r8&hw=i7&test=db))
I think Django is almost certainly a superior alternative. It comes with a
built-in authentication and group/role-based authorization system, a built-in
'admin' system that can be used to serve up basic CRUD forms for your models
with minimal effort, and the ability to code custom 'reports' as add-ins to
the built-in admin system with only minimal additional effort. If your models
aren't terribly complex you could have a test-ready basic CRUD app matching
your description in less than 30 minutes, 90% or more of the code for which
will have been generated (or come pre-packaged) for you.

------
usamec
High quality PHP does not exists, since PHP is ultra low quality language:
[http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-
de...](http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/)

~~~
nkuttler
You're right, but the post wasn't really about PHP itself. Of course, with
this in mind, PHP apps can never reach high quality.

------
fowkswe
I think a valid point to make here is that what has propelled Ruby, Python,
JS, etc, forward has not been the language, but the frameworks that have put
context and boundaries around using those languages.

Its entirely possible to write a poorly organized bowl of spaghetti using Ruby
if you leave Rails out of the picture. I would venture to say the same is true
for any programming language.

PHP was born before the idea of an open source framework had been propagated,
it spread around the web before many of the paradigms and patterns had evolved
to what they are today, and unfortunately it failed to gain acceptance by the
elite programmers that have carved a community out using those other
languages.

The other languages' respective frameworks have large, organized communities
which have attracted very smart people that have put large swaths of
functionality in place, before you ever have to write a line of code.

Its playing catch-up now, but there are good examples of PHP out there and it
most certainly can be used in an elegant, high-quality manner.

~~~
sixthloginorso
> Its entirely possible to write a poorly organized bowl of spaghetti using
> Ruby if you leave Rails out of the picture. I would venture to say the same
> is true for any programming language.

You can easily write one with Rails over just a few months of maintenance. I
hate, _hate_ having to do a damned autopsy to trace all the @instance (read,
global) variables copied and inserted into the objects that you never create
yourself, -because Rails devs are apparently above having to endure the
savagery of passing arguments to their methods- and having to grep every
single function that gets automatically included from everywhere in the
project into the namespace.

------
heron87
I believe "high quality PHP" is an oxymoron. It's just impossible to be aware
of all the pitfalls PHP contains. Here is just one example among many:

> if( '33333333333333333333' == '33333333333333333334' ) echo "equal"; Output:
> "equal"

The "reason" for this very odd behaviour is that the two strings are
automatically converted into integers, which leads to an int overflow, which
PHP ignores (ignoring errors is very PHP specific) by guessing that the two
"numbers" must be equal. I'm sure they even call this a feature...

~~~
jiggy2011
I just tested this (PHP 5.4) and I don't get this behaviour. In this example
the two are compared as strings not integers so are not equal.

PHP does not automatically convert types unless you do something like this:

if( 33333333333333333333 == '33333333333333333334' )

But here the problem is the integer overflow not specifically the type
conversions. You are going to get weird behaviour no matter what you do
because you don't have enough bits to store that number.

If you want to use numbers this size then you are going to need some type
other than the standard int.

~~~
DCoder
Not entirely true. PHP definitely tries to perform a numeric comparison on two
strings if it thinks they are both numerical strings, but this behaviour
changed in PHP 5.4 [0]. heron87's scenario was reported as a bug [1], changed
in PHP 5.4 and caused new problems [2].

See the string comparison routine in 5.3 [3] and 5.4 [4]. The new
implementation _still_ inspects the strings to see if they are numeric and
then tries to do a numeric equality check rather than string equality check,
leading to amusing tricks like

    
    
         var_dump("0xFF" == "255.0"); // true!
    

But if both values overflow the long type and their double representations are
equal, the new implementation falls back to actual string equality. A quick
check at [5] shows that both values heron87 compared have identical double
representations, assuming (sizeof double == 8), so the new representation
detects the long overflow, then it sees that double representations are also
identical, and then it returns to string equality rather than numeric
equality. The old implementation did not consider long overflow and ended up
comparing the numeric values.

[0] [http://3v4l.org/a0nKq](http://3v4l.org/a0nKq)

[1]
[https://bugs.php.net/bug.php?id=54547](https://bugs.php.net/bug.php?id=54547)

[2]
[https://bugs.php.net/bug.php?id=62097](https://bugs.php.net/bug.php?id=62097)

[3] [https://github.com/php/php-
src/blob/PHP-5.3/Zend/zend_operat...](https://github.com/php/php-
src/blob/PHP-5.3/Zend/zend_operators.c#L1992)

[4] [https://github.com/php/php-
src/blob/5.4/Zend/zend_operators....](https://github.com/php/php-
src/blob/5.4/Zend/zend_operators.c#L2039)

[5]
[http://babbage.cs.qc.cuny.edu/IEEE-754/index.xhtml](http://babbage.cs.qc.cuny.edu/IEEE-754/index.xhtml)

