Last Updated: February 25, 2016
·
2.57K
· yuvi

Why I hate PHP (or: how I learned to cope using PsySH)

I hate PHP. I might've mentioned that already.

The reason I have to hate it are many, and I wouldn't be the first to point them out. Instead, I'd like to focus a minute on why I find PHP a bad programming language for anyone - and I'm going to (try) avoid talking specific little technical issues (like those in the linked article) and concentrate on the bigger picture. I'll try to make things as easier to understand.

Finally, I'm also actively using PHP. Yes, it's hilarious, poke fun at me. It's still the most used language for web development, and a lot of companies use it, including, so it happens, the one I work for. While I do try to concentrate on the front-end work I have, building plugins and whatnot, I can't avoid doing a little PHP work here and there. And if you're in a similar predicament, then good news! I learned how to cope with it. Kinda.


So first off - what's wrong with PHP you ask?
Well, it boils down to 3 main issues:

  • Inconsistent syntax and bad OO design
  • bad architecture
  • security issues

I won't go to details on the last one (there's enough said about it anyway).

I'm a python guy. I love Django, I did some projects with it. I also love javascript so node.js is great. And I hear good things about Ruby-On-Rails so I guess good for them too.

What all those languages have in common is consistency between procedural code (using functions etc.) and OO (Object-Oriented) programming. Even if you don't understand what is object-oriented, even if you never use it - you still do, somehow. Because it's ingrained in the languages.

for example, in Python and JS, you can learn how to work with lists. Let's try something simple like joining a list.

>>> l = ['a', 'b', 'c']
>>> l.join('-')
a-b-c

See that? Easy. doing it in JS is nearly identical (except for using var). Now let's try PHP:

php > $l = array('a', 'b', 'c');
php > echo join(',' $l);
a-b-c

At face value, it doesn't seem like a big deal. But it is - because there's a huge difference here. Can you see it? If not, then read this aloud dramatically like it's a really big deal (even if you don't get why straight away):

Python and JS use a method while PHP uses a built-in function

Now that may seem not important but it's a big problem. Because when you start developing bigger, you need better architectural tools. And PHP doesn't have them.

When you start working with Classes in Python, it feels very natural - because it's a direct continuation of what you were already using. When you create a class and a method for the first time, using join suddenly makes sense - hey, you say to yourself, I get it! It's a method! list is an object! everything makes sense! Programming is fun again. Look, it's the same syntax:

class House(object):
    def __init__(self, color):
        self.color = color

    def print_house(self):
        print( "I am a house, my color is " + self.color )


myhouse = House('green')
myhouse.print_house() #similar to l.join(...) - using a dot

Yes, there's a bit of a learning curve and differences (like understanding how to use self). But PHP? No, it has a completely different syntax here, that looks even more retarded than associated arrays (somehow):

class House {
    public $color;    

    function __construct($color) {
        $this->color = $color;
    }    

    function printHouse() {
        echo "I am a house, my color is " . $this->color;
    }
}

$myhouse = new House('green');
$myhouse->printHouse();  // doesn't look like anything we've used before

Hey, what are those completely new arrows that are unrelated to anything else in the language and are not something I have ever used before?

And this isn't a small issue of getting used to the new syntax. Because the entire language is built around using a procedural approach and not OOP, and it doesn't help matters that OOP is 13% less performant. The whole thing feels like it's a patch. Maybe because, well, it is.

Now to be fair - Python's OO is also sort of a patch, but it's still consistent throughout the entire language. And it introduces another implication - because if you choose not to use OO in PHP (for the various reasons between needing to learn it, being slower, etc.) then you're left with relying on a list of predefined functions, most of which have confusing names and behavior, and if that's what you're doing - you're not learning a language. you're memorizing a shopping list.

It's also what makes it hard for OO to actually develop any further - In JS I can easily add another method for the string object - i.e., if I have a list of methods for string, I can overwrite the behavior of one of them, or add a new one. In PHP it's nearly impossible (It's... ugly) - because the language didn't expect that.

Eventually, it goes down to the core of it, really - PHP was designed to build small simple websites. And that's it. It came early on, when server-side was just becoming a thing, and it focused on simple stuff, and it's what it does best. It was good for it then, and it never really progressed a lot. partly because it didn't have an actual OO design, it was (and still is) just a bunch of shortcuts.

Want to build something big and awesome? Prepare to work hard looking for workarounds, because it's hard to configure a complex PHP website without resorting to hacks. There's one php.ini, and if you want to add another one for a specific website, you need to define it from scratch. You need to put all your source files on your server (which might lead to serving the actual source files - again, not very secure). Because this language is all about telling you to screw yourself.

And it ends with badly designed websites. So don't use it. If you only want to write a small file and run a site, go for it, but if you plan to actually be good at some point, go with node.js, RoR, Django (or Pylons, whatever), even .NET (actually no, not really. don't use .NET).

Forget about PHP, for a better, smarter, and more secure future for web development. Do it, for our children (please add dramatic music and maybe an explosion in the background for full effect).


And that brings me to PsySH (let's all vow to pronounce it PSI-SH from now on, because that's fantastic). For the sad bunch of us who have to still use php, this is perfect. It's the ipython of PHP. No, it's more than an interactive console. It's a REPL that also works as a debugger and has awesome stuff like reflection, documentation and pretty colors. Just... go. use it. It almost makes it bareable.

p.s.
If you plan on leaving a comment, I would love it! Tell me what you think and why I'm completely wrong, an idiot bla bla bla, whatever. With the right arguments, you might even change my mind. Just not with these things:

  • I like swearing a lot, so you can do it too. But don't swear... you know, at me. Thanks.
  • I will not appreciate comments like those mentioned here for the same reasons he explains there, and will ignore them.
  • If you never used anything else other than PHP (and I mean programming languages, HTML, CSS and stuff like that don't count), then you're not qualified to leave a comment. Sorry, but you have to have a little experience with something else before you can tell me PHP's better.

That's it! Hope it's enjoyable\helpful\spite-inspiring

6 Responses
Add your response

Hey you are like a little crying boy, that blames on the ball since he doesn't know how to use it.
Are you really think that PHP has a bad architecture or maybe your app is badly designed?
You don't like syntax, but it is not a reason for saying that it is a bad langs.
And say me, what a problem to change the job and change the developing lang from PHP to smth else?
Every programming lang has it's own advantages&disadvantages. And in my opinion, it is not a bad lang, it is a bad developer that doesn't realize how to use it in the proper way.

PS. I just imaging how would you yelling on the assembler if you try it. xD

Yours Egor (dev: football.com)

over 1 year ago ·

So you're complaining about syntax differences between languages? In other news, water is wet.

over 1 year ago ·

@egor_igorevich
I have specifically asked that you avoid this type of comment, to quote the post I linked:

"Do not tell me that “good developers can write good code in any language”, or bad developers blah blah. That doesn’t mean anything. A good carpenter can drive in a nail with either a rock or a hammer, but how many carpenters do you see bashing stuff with rocks? Part of what makes a good developer is the ability to choose the tools that work best."

over 1 year ago ·

@milesj
No. Read before commenting. I am complaining that PHP alone has two different syntaxes, which are inconsistent with one another

over 1 year ago ·

"Bad security" in a language context sounds wrong. Security should be done at application level, not at language level.

over 1 year ago ·

@vimishor

I don't see why security in a language context is necessarily wrong. Saying that it should only be done at the application level is like saying that memory management should only be done at the application level, and not the language level.

Indeed, although a bit rarer these days (since garbage collection has pretty much won, for most purposes) C and C++ advocates have often bashed Lisp, Java, Python, PHP, et al. precisely because you can't control how much memory you use at a given moment. However, manual memory management is a major source of bugs, including security holes, so deciding to use garbage collection is deciding that the language should take steps for better stability and even security.

Thus, doesn't it make sense to ask what else a language might be able to do to ensure security? (In the case of MySQL, we already have one answer: have special constructs that will automatically escape things for you when you enter data into the database. Even PHP has such structures, although they aren't available by default!)

over 1 year ago ·