Live data from Hacker News

Coding Horror: The PHP Singularity

codinghorror.com

221–230 of 341 posts

Re: Coding Horror: The PHP Singularity

#221
I wish bloggers would stop quoting that fractal article. At least 50% of what's written in there is totally wrong/false. Other information is terribly out of date. And even more information is merely half-truths and lack of understanding of the language. The article author clearly scanned through PHP bashing articles and took material from them verbatim; mistakes and all.

I'm not going to argue that PHP is a great language, but the article is a complete disservice to anyone who has bothered to read it. I'm disappointed, but not surprised, that Jeff Atwood linked to it.

Just a few errrors in that article:

> Operators are very fragile in the parser; foo()[0] and foo()->method() are both syntax errors. The former is allegedly fixed in PHP 5.4, but I can’t find mention of a fix for the latter.*

The latter doesn't need a fix because it always worked. Honestly, how hard is it to test that foo()->method() works?

> Objects compare as greater than anything else… except other objects, which they are neither less than nor greater than.

Strict-equals on objects compares the references; but regular equals compares the contents of the objects. Two objects compare equal if the contain exactly the same fields and values. Seems pretty reasonable to me.

> + is always addition, and . is always concatenation.

This is a good thing; JavaScript gets this wrong.

> There is no way to declare a variable. Variables that don’t exist are created with a null value when first used.

Variables that don't exist issue a notice. You can deal with that just like any other error.

> Global variables need a global declaration before they can be used.

Actually there is also the $GLOBALS array for this. I'll agree that's not much a solution. Globals should just not be used; if you want to use static class variables, it's a much better choice with a sane syntax.

> there’s no pass-by-object identity like in Python.

I'm not sure if I understand this but all objects are passed-by-reference in PHP (since 5) and PHP references act appropriately when used as function parameters, etc.

> A reference can be taken to a key that doesn’t exist within an undefined variable (which becomes an array). Using a non-existent array normally issues a notice, but this does not.

An attempt to use the reference will result in a notice but isset() and empty() operate it on it correctly.

> Constants are defined by a function call taking a string; before that, they don’t exist.

You can declare constants in classes and namespaces with the const keyword.

> There’s an (array) operator for casting to array. Given that PHP has no other structure type, I don’t know why this exists.

You can cast scalars to single element arrays and objects to arrays with the same structure. Both are actually very useful.

> include() and friends are basically C’s #include: they dump another source file into yours. There is no module system, even for PHP code.

PHP is interpreted -- namespaces and autoloaders are PHP's module system.

> Appending to an array is done with $foo[] = $bar

This is a good thing.

> empty($var) is so extremely not-a-function that anything but a variable,

Empty is equivalent to the not operator but will also work on undefined variables -- that's why it requires a variable.

> There’s redundant syntax for blocks: if (...): ... endif;, etc.

Useful inside of templates where matching { } is much more difficult.

> PHP’s one unique operator is @ (actually borrowed from DOS), which silences errors.

Sometimes you don't care if a function succeeds; like with the unlink() function which will raise an error if the file you're trying to delete doesn't exist.

> PHP errors don’t provide stack traces.

Not true. Debug_backtrace() will give you a stack trace in an error handler.

> Most error handling is in the form of printing a line to a server log nobody reads and carrying on.

Assuming, of course, the programmer doesn't do anything to handle errors.

> E_STRICT is a thing, but it doesn’t seem to actually prevent much and there’s no documentation on what it actually does.

E_STRICT (or lack of it) is for compatibility with PHP4. When enabled it will "warn you about code usage which is deprecated or which may not be future-proof." -- quote from the manual.

> E_ALL includes all error categories—except E_STRICT.

Unfortunate naming here -- E_ALL is from PHP4 and prior and E_STRICT is all about PHP5. Including it in E_ALL would break PHP4 scripts running on PHP5.

> Weirdly inconsistent about what’s allowed and what isn’t.

This author is confused why syntax errors would be parse errors but logic errors are not.

> PHP errors and PHP exceptions are completely different beasts. They don’t seem to interact at all.

This is sort of true; PHP errors and exceptions exist in different universes but it's easy to unify them and PHP even provides a built-in exception ErrorException to do so. You can turn every PHP error into an exception with 4 lines of code complete with stack traces. You could even turn exceptions into errors but I wouldn't recommend that. PHP supports both procedural and OO programming styles -- this is not a bad thing.

> There is no finally construct

C++ also doesn't have a finally construct. But C++ and PHP support RAII -- class destructors run when the stack is unwound so you can do your cleanup. Finally would be a welcome addition to both languages.

> function foo() { return new __stdClass(); } leaks memory. The garbage collector can only collect garbage that has a name.

PHP is reference counted with a cycle-detecting GC. That would not leak memory.

> Function arguments can have “type hints”, which are basically just static typing. But you can’t require that an argument be an int or string or object or other “core” type

This is true, but it's an ongoing discussion on how to correctly handle scalar type hints. For all the discussion about how PHP isn't designed the author takes issue with the thing they're taking their time on.

> Closures require explicitly naming every variable to be closed-over. Why can’t the interpreter figure this out?

Because of the dynamic abilities of PHP, there is simply no way for the interpreter to ever figure out the variable to close over. The solution is actually a rather simple.

> clone is an operator?!

Of course!

> Object attributes are $obj->foo, but class attributes are $obj::foo. I’m not aware of another language that does this or how it’s useful.

C++ does it. $obj::foo doesn't make any sense, if you're accessing class attributes then you use the class name Class::foo.

> Also, an instance method can still be called statically (Class::method()). If done so from another method, this is treated like a regular method call on the current $this. I think.

Only static methods can be called statically. The other calling methods statically is similar to C++ ... you can call parent class methods explicitly by name by-passing any overriding.

> new, private, public, protected, static, etc. Trying to win over Java developers? I’m aware this is more personal taste, but I don’t know why this stuff is necessary in a dynamic language

This is personal taste not a valid critique.

> Subclasses cannot override private methods.

That is the definition of private methods!

> There is no method you can call on a class to allocate memory and create an object.

You can use reflection to create an object without calling the constructor.

> Static variables inside instance methods are global; they share the same value across all instances of the class

This is the definition of a static property!

> Yet a massive portion of the standard library is still very thin wrappers around C APIs

That is, in fact, the point. PHP is supposed to be a thin scripting language layer over C. It's expanded beyond that. Many of the poor naming conventions are not because of PHP but rather are the exact API of the underlying C library.

> Warts like mysql_real_escape_string, even though it has the same arguments as the broken mysql_escape_string, just because it’s part of the MySQL C API.

Both the C API and PHP have both these functions for backwards compatibility reasons. This entire API is pretty much depreciated with both the mysqli library and PDO replacing it.

> Using multiple MySQL connections apparently requires passing a connection handle on every function call.

Yes, exactly. That's the only way multiple connections could possibly work.

> PHP basically runs as CGI. Every time a page is hit, PHP recompiles the whole thing before executing it.

Unless you use a free code cache like APC. It will eventually be built in. Most people don't need it.

> For quite a long time, PHP errors went to the client by default

If you don't handle your errors, they go somewhere.

> Missing features

Most of these are provided by frameworks just as they are in Python, Ruby, C#, etc.

> Insecure-by-default

Most of these things are now removed from the language after being depreciated for years.

Re: Coding Horror: The PHP Singularity

#223
Since lately folks have been attacking the people instead of their arguments I also have to preface my comment.

I am a full time Rails Developer. So here we go...

If you want to produce free-as-in-whatever code that runs on virtually every server in the world with zero friction or configuration hassles, PHP is damn near your only option.

That is the crux of this whole debate summed up in one beautiful sentence. I almost cried.

Until someone builds a better solution, where better includes as easy or easier to setup on than php. Php is going to be the defacto standard.

And for the love of God please don't say well all you have to do install Blub then configure foo server and...

You've already failed to be easier to get started on than with php. And if you want php to go away you have to do just that.

Where do I install Blub? What happens when there are 30 different servers that can run it which one do I pick? Why? What are the trade offs? Version Y is on the website but everyone really uses version X because the the dependencies haven't been up dated to Y yet. And the major framework just had a huge fork/merge/catfight and now its future is uncertain ...

Fuck it I can pay Shithost $5 a month for php and I can toss a few variables in this html crapped out.

Your average person who needs something to show up on a website

and lets be very clear if you want to see php go away these are the people you need to go after

does not care about: - lambdas

- good language design

- namespacing etc,

they don't read Jeff Attwood, Hacker News or any of that.

One thing the php community as a whole has always focused on is ease of getting started. Take Wordpress. The 5 minute install? That gets up an running an allowing me to do what I care about (getting my client's content on the internet) I still use it long after converting to other languages for most development.

If you can't compete with that it does not matter how many templating languages or css precompilers or what the hell ever you got. You don't stand a chance.

TL, DR: Long Story Short. php and its children make it easy to drop in and play. Thats what you are up against. Not language design beauty/ purity or features. Ease of getting started.

Because honestly the only people who care enough to switch are people who care enough to overcome that in other languages.

Re: Coding Horror: The PHP Singularity

#224
post #125

Earlier quoted context omitted.

Just needs a bit of type system magic. Don't use the same type for escaped and unespaced strings. (And don't use the same type for user generated input before and after it's scrubbed / escaped of any nastiness.) Ask any Haskell weeny for details. Also in your example, you'd probably be better of, if your language knew about the HTML structure, e.g. something like P($var), instead of putting the tags in as strings.

> Don't use the same type for escaped and unespaced strings. And if you can't extend your type system to make this work, do it in your head, mutating the names of variables to help you keep it straight. For example, esStr and unStr are not of the same type, and moving data from one to the other without conversion is always an error.

Which was one of the original and useful points of (apps) hungarian notation.

Re: Coding Horror: The PHP Singularity

#225
post #125

Earlier quoted context omitted.

Just needs a bit of type system magic. Don't use the same type for escaped and unespaced strings. (And don't use the same type for user generated input before and after it's scrubbed / escaped of any nastiness.) Ask any Haskell weeny for details. Also in your example, you'd probably be better of, if your language knew about the HTML structure, e.g. something like P($var), instead of putting the tags in as strings.

> Don't use the same type for escaped and unespaced strings. And if you can't extend your type system to make this work, do it in your head, mutating the names of variables to help you keep it straight. For example, esStr and unStr are not of the same type, and moving data from one to the other without conversion is always an error.

This reminds me of Charles Simonyi's classic article on Hungarian Notation. I know that style gets criticized a lot, but that's usually when it has been used inappropriately. If you have a language with a weak type system then a sensible variable prefix convention can help a lot.

http://msdn.microsoft.com/en-us/library/aa260976(v=vs.60).as...

Re: Coding Horror: The PHP Singularity

#226
post #46

Earlier quoted context omitted.

Okay, not directed at you, but... It was posted yesterday. Does this really have to be posted EVERY SINGLE DAY? Every time anyone suggests improving anything, or make another product in a crowded space, this comic is posted. The author of this article doesn't even suggest a new platform - he suggests improving a platform that exists to enable easier deployment. It was posted yesterday for Chocolat... do we really hav…

Well, not exactly. The author does suggest a new one: > I'm starting a new open source web project with the goal of making the code as freely and easily runnable to the world as possible.

Nope. In this line he's talking about a web app he's writing and how he considered writing it in PHP to boost adoption. Here's it in context:

"I'm starting a new open source web project with the goal of making the code as freely and easily runnable to the world as possible. Despite the serious problems with PHP, I was forced to consider it."

Re: Coding Horror: The PHP Singularity

#227
post #73

Ok, so here it goes. I haven't commented on this site for over a year. Full disclosure: I am an average programmer compared to many here. I come to this site to improve my self and read about what brilliant and amazing things you all do. Now you have a reference. I use PHP on a daily basis, have been for years. I am a freelance developer who has been able to carve out a living with a lot of hard work and a lot of luc…

"Finally, let's use his tool analogy. PHP is a double-clawed hammer. What if your job was to remove nails from wood all day?" Brilliant.

>Brilliant.

Except that, if you're removing nails, there are much better tools than the claw on a hammer. [1][2] I've done demolition, and I've had to pull a lot of nails, and the claw on a hammer is about the last tool I'd use for the job. I basically only use the claw to straighten or remove a partly-hammered nail that's gone wrong.

So yes, the analogy is brilliant.

[1] For really big nails, more leverage is good, and the hole in the flat part can be used to pull longer nails out than you can pull with a hammer claw: http://refer.ly/abwW

[2] To get a nail started, something sharper with points, and that (here's the important bit) can be HAMMERED under the nail head: http://refer.ly/abw2

Re: Coding Horror: The PHP Singularity

#228
I'm a PHP guy (started in the PHP3 days) and a few weeks ago I decided to finally have a go at building a non-trivial site using uWSGI, Flask, sqlalchemy and mongrel2. I knew a bit of python (from having written a few trivial sysadmin related scripts) but apart from that I was starting cold. I'm now a few weeks in and while I'm not ready yet to jump ship back to my PHP comfort zone, I can say I've been surprised to find that Python (one of the "better, grown up" languages that's always cited in these PHP bashing threads) has quite a few warts of its own:

- Packaging in python seems to have been a nightmare up until quite recently. It appears things are a little more sane now that pip is emerging as the new standard but there's still documentation all over the web referencing easy_install. Of course, then there's distutils2 to muddy the waters...

- The Python 2 to Python 3 transition was and continues to be an almighty upheaval. At the moment huge swathes of the python ecosystem have yet to introduce python3 support (e.g. Flask).

- Virtualenv is a fairly ugly hack in itself but even worse than that, trying to move a virtualenv directory somewhere else on the file system turns out to be impossible unless you use another third party tool.

- The python language has no switch statement. I refused to believe it when I first discovered this but it is actually true.

So, two weeks or so in and those are my main gripes thus far. Apart from that, I'm enjoying python. My favourite "python thing" discovered so far is all the cool list iteration syntax.

Re: Coding Horror: The PHP Singularity

#229
post #161

Earlier quoted context omitted.

I have to ask, because it's really been getting at me for a while honestly. How do your developers react to that? I mean, you guys are known for going after the best and the brightest. People who have worked hard to perfect their craft. These people tend to have strong opinions when it comes to language design. How do you get them from that point to "ok, now write some php code" ?

That's easy: the best recognize languages are just a means to an end. Language snobbery, in my experience, tends to be highly correlated with being a "wannabe" rather than "great" and a useful filter. You will find any number of C/C++ programmers who think you can't write anything in Java. Many Java programmers think you can't write anything (beyond 100 lines) in Python. Many Python programmers think you can't (or si…

That's a well-reasoned but rather lazy response imo.

Ppl who have strong opinions when it comes to language design have strong opinions when it comes to language design. Ppl who think languages are just a means to an end think languages are just a means to an end. These are two differenty species and indicative of their particular worldview, attitude and preferences. To use that as any kind of filter is silly.

Lets not look at CS for a second...If you want to know what a Stieltjes integral is, a probabilist will tell you one thing, a functional analyst another, and a measure theorist will give you yet another more pedantic definition. There's no right or wrong definition here...every one is coming at it with the tools they are more well versed in. The probabilist is the end-user, so to speak. He can't do his day to day work without internalizing it, because his bread and butter, the PDFs and CDFs of all the distributions he deals with, comes from Stieltjes. The functional analyst is going to use Stieltjes when he deals with Banach spaces, but otherwise he has other preoccupations. The measure theorist studies Stieltjes in depth because it goes to the very foundantion of measure theory...but he isn't really an end-user.

Now back to CS...a C++ programmer will think of Java as an interesting toy because you don't get union types and pointers & so forth. The Java guy will think of Python as an interesting toy, and the Python guy thinks PHP is an interesting toy. None of this means any of these people are insecure or being a teenager. If the only goal in life is "languages as means to an end" where end = $$$ in the short term, then yeah, lets just cut to the chase and do PHP. But there are other goals, yeah ?

Re: Coding Horror: The PHP Singularity

#230
post #48

I seriously groaned when I saw this post (title). I was expecting an elitist diatribe about PHP (because this is perennially popular amongst particular programmers) but that's not what this post is intended to be. Interestingly Jeff does take the usual potshots at PHP almost like he thinks he'll lose street cred if he doesn't but the basic message I agree with: if you want someone to stop doing something you consider…

> 3. Imperative programming is IMHO a natural fit for Web programming.

I disagree with this, especially in a RESTful world. It is very convenient, and obvious, way to look at a web request as a referentially transparent function for the most part. And the non referentially transparent requests really do change the world. We are moving our backend to a bunch fairly clear workflow of, that has layers that look like:

Get DB stuff -> referentially transparent -> Write DB stuff

And these can be arbitrarily nested. It works pretty well, it's easy to reason about, and it's pretty easy to write.

Post reply on HN