KBD

Keith Devens .com

Wednesday, July 9, 2008 Flag waving
That is because you crazy! – Ikea
← References and pointers"I'm much better-looking online" →

Daily link icon Wednesday, August 13, 2003

I hate PHP

I hate PHP... just hate it. Hate it hate it hate it.

The newest thing I've been dealing with is the idiotic call-time pass-by-reference deprecation. I've never heard the reasoning for the deprecation, and as far as I can tell, there isn't any. I should start keeping a list of all the things that become impossible without it...

  • You can't have a default argument in a function that's passed by reference: function ($foo, &$bar = NULL){} doesn't work, although it will in PHP 5. Great, make alternatives available before you deprecate features, assholes.
  • Want to append a reference to an array using array_unshift, array_push, etc.? array_unshift($array, &$value) is no good. The only solution for array_unshift is to create new arrays and manually shuffle everything around.
  • Want to allow a callback to be called using call_user_func and have its arguments passed by reference? You're out of luck again. The only solution is to wrap a reference to your argument in an array and pass that... and your callback functions have to be rewritten to take arrays.
  • Finally, the worst example, which has caused me to waste literally hours trying to fix it is this: I've been removing call-time pass-by-reference from my XML-RPC library, because it's deprecated and people keep getting error messages when using my library (which isn't good). So, I tried to remove call-time pass-by-reference stuff a long time ago when I started working on version 3. So now I started working on the XML parsing part of it, which happens to use xml_set_object. Check out the documentation for that... it uses this line: "xml_set_object($this->parser, &$this);" Which, of course... **IS DEPRECATED**

I'm so mad my head's going to explode... I've been trying very hard to avoid littering this post with four letter words. What this last example shows is that xml_set_object is completely useless without being able to pass an object reference in there, since typically you'll store instance data representing your XML data in that object, and if you don't pass $this by reference you'll wind up with copies all over the place, which is what was happening to me. I think the solution to this is to pass array(&$this, "callback") to xml_set_element_handler, so we'll see.

The last example should work in PHP 5 as well, since objects will be passed by reference and not by value (which was another damn huge design mistake in the first place). But they deprecated a feature, which causes everyone to get big ugly error messages (not even NOTICEs, but WARNINGs) under the default install, without providing any workarounds or replacements.

PHP is a horrid language that I never want to have to do any development with ever again. Unfortunately, there are really no alternatives available that are widely deployed on shared hosts.

← References and pointers"I'm much better-looking online" →

Comments XML gif

Simon Willison (http://simon.incutio.com/) wrote:

THANK YOU! I had the weirdest bug in an XML parsing class I wrote recently: for some reason, calls to update and check a property of the class just weren't working. I spent 2 days pulling my hair out about it before cludging it with a global variable (ugh) and moving on. I've just added the & to the $this in my xml_set_element_handler calls and now it works fine.

I am so looking forward to PHP5.

∴ Simon Willison | 13-Aug-2003 12:09pm est | http://simon.incutio.com/ | #2695

Keith (http://www.keithdevens.com/) wrote:

Heh, then maybe you can help me get mine to work... Smiley

Keith | 13-Aug-2003 12:17pm est | http://www.keithdevens.com/ | #2696

Keith (http://www.keithdevens.com/) wrote:

The worst thing about all this is that I had (and still have, of course) code that works, but that I have to waste hours of my life rewriting because someone (apparently arbitrarily) decided to deprecate a feature without considering the consequences.

Keith | 13-Aug-2003 12:20pm est | http://www.keithdevens.com/ | #2697

Ben (http://www.trollscript.de/blog) wrote:

Well, I can relate to the "reference angst" in PHP. It's currently very confusing, though it's not a very new situation. Reference support until now has always been sketchy and impartial. What strikes me particularly as odd is the relationship between arrays and references. For instance,

$foo->test(array(&$bar));

where bar is an object does NOT trigger the default error warning. I don't know if it rather should or not, or if it's a perfectly legal PHP statement. Anyway, wrapping a one-element array around the variable enables provides a kludge for optionally passing references, if test() looks like this:

function test($param = null) {
if (isset($param)) {
$param[0]->add('hi');
}
}

PHP horrid? Perhaps. Inconsistent? Indeed.

∴ Ben | 13-Aug-2003 12:27pm est | http://www.trollscript.de/blog | #2698

Moss Collum (http://www.m14m.net/) wrote:

I hate PHP

Now there's something all of us, liberal or conservative, can agree on.

∴ Moss Collum | 13-Aug-2003 12:57pm est | http://www.m14m.net/ | #2701

Keith (http://www.keithdevens.com/) wrote:

Definitely inconsistent. So much so that I constantly have to look to the reference to find out which functions take parameters in which order, since "needle" and "haystack" are so often reversed.

"Horrid" includes other major pain in the butt features like magic_quotes, trans_sid, etc.

Keith | 13-Aug-2003 1:01pm est | http://www.keithdevens.com/ | #2702

82.36.196.38 wrote:

Ben,

The reason for that is that a copy of the array is made (including a copy of the reference).

Inconsistency and bad planning are my two main gripes with PHP. Magic quotes, for example. "I know, let's put backslashes into strings from the user, just on the off-chance that the author is going to dump them into a database without quoting them! Who cares about sane authors?"

Not to mention the fact that it's impossible to use nl2br() with current versions of PHP and valid HTML (it only generates XHTML-style br elements).

∴ 82.36.196.38 | 13-Aug-2003 1:11pm est | #2703

Keith (http://www.keithdevens.com/) wrote:

Not to mention the fact that it's impossible to use nl2br() with current versions of PHP and valid HTML (it only generates XHTML-style br elements).

Yeah, and highlight_string, et al, still use <font> tags to do the highlighting. In my markup library, where I want to allow PHP code highlighting, I had to do a regular expression replacement on the generated string to replace '<font color="' with '<span style="color: ' and '</font>' with '</span>'.

Keith | 13-Aug-2003 1:18pm est | http://www.keithdevens.com/ | #2704

Keith (http://www.keithdevens.com/) wrote:

Actually, I don't know what your problem is with XHTML style <br /> tags. AFAIK, they should work with everything... that's a feature, not a problem. I didn't read your comment carefully enough originally.

Keith | 13-Aug-2003 1:20pm est | http://www.keithdevens.com/ | #2705

Keith (http://www.keithdevens.com/) wrote:

Grr... one more correction. I didn't do a regular expression replacement like I said above... just a plain string replacement.

Keith | 13-Aug-2003 1:27pm est | http://www.keithdevens.com/ | #2706

Sterling Hughes (http://www.edwardbear.org/blog) wrote:

call time pass by reference is sometimes necessary, in this case just use it. You'll only get these errors if you are using E_NOTICE. E_NOTICE just tells you cases where you could be doing something that is possibly wrong, not necessarily wrong.

Its sorta like: "Tell me when I might be doing something stupid mode." It doesn't mean you actually did something wrong.

Most of the object inconsistencies will be fixed in PHP5, btw.

∴ Sterling Hughes | 13-Aug-2003 2:34pm est | http://www.edwardbear.org/blog | #2707

Keith (http://www.keithdevens.com/) wrote:

At first I thought they were notices, but since the error messages said "Warning:" and not "Notice:" I figured they were E_WARNING level messages. Thanks Sterling, I may just go and turn off E_NOTICE's for those and save myself the headache.

The problem, for people like me who want to write libraries, is that we can't just worry about our own configurations. So for every function I may have to turn off error_reporting() of E_NOTICES. Smiley

Sterling, do you know why call-time pass-by-reference was deprecated in the first place?

Keith | 13-Aug-2003 3:40pm est | http://www.keithdevens.com/ | #2708

Harry Fuecks wrote:

If you're looking for some entertainment, how about trying HTMLSax instead?

Thanks to some great input via Sitepointforums, managed to get it ramped up to be almost as fast as the native PHP XML extension. I've also done my best to make the API as similar as possible so it should mean only minor modifications to a class currently using Expat. Thats the downside though - the handlers must be available via an object. Oh and HTMLSax wont warn you if the XML was badly formed.

But $parser->set_object($this) will mean $this is referenced...

∴ Harry Fuecks | 13-Aug-2003 7:41pm est | #2710

Scott Johnson (http://feedster.com/blog/) wrote:

Hi Keith,

Well I'll definitely agree with you on php's inconsistencies. They suck. But different languages work for different folks. PHP works for me so I'll stay with it. It does sound like you're just not happy and maybe Python is a better place for you (though it would be a shame to lose you although I'll benefit since I'll learn from your learning process (and blogging about it)).

Best
Scott

∴ Scott Johnson | 14-Aug-2003 6:59am est | http://feedster.com/blog/ | #2712

anand (http://masterofweb.com) wrote:

Personally I prefer ruby though I do a lot of plugin development in PHP for nucleus.

∴ anand | 15-Aug-2003 11:05am est | http://masterofweb.com | #2718

Keith (http://keithdevens.com/) wrote:

Just wanted to update this post: The errors given to complain about call time pass by refererce are definitely E_WARNINGs and not E_NOTICEs.

Keith | 17-Dec-2003 1:28am est | http://keithdevens.com/ | #3580

Markus wrote:

about PHP being inconsistent, I can only agree

see http://tnx.nl/php.txt for more

∴ Markus | 24-Dec-2003 9:54am est | #3623

Keith (http://keithdevens.com/) wrote:

Hey, I think that's the article I linked to here, but the link broke almost immediately.

Keith | 24-Dec-2003 12:47pm est | http://keithdevens.com/ | #3626

Hong (http://www.k-edge.com) wrote:

Workaround for "Call-time pass-by-reference has been deprecated":

call_user_func_array(array(&$obj,$method),array(&$arg1,$arg2));

∴ Hong | 2-Apr-2004 9:52am est | http://www.k-edge.com | #4283

Jeff wrote:

I find it funny that this site is powered by PHP.

∴ Jeff | 7-Jul-2004 2:05pm est | #4941

Keith (http://keithdevens.com/) wrote:

Keith | 7-Jul-2004 2:16pm est | http://keithdevens.com/ | #4942

tomm wrote:

I just was curious about PHP, since our apprentice loves it a lot... So far, I resisted to touch anything else than Perl and Bourne Shell, and I'm happy with it. "Give the customer what he wants, not what he needs" - oh god, I'm sick of it. I've seen guys claiming to be pros in any known language. The first Perl snippets I encountered said all enough...

Ah, PHP is perfectly suited for the unexperienced, wannabe hacker. Yes, you get some results quickly. Just an example which gave me a really bad impression about PHP: Compare PHP's DB stuff with Perl's DBI interface.

Stick at your programming language of your choice, and really learn how to code. Keep it simple (not stupid, simple Smiley. Write concepts. End up with clean, well commented code, and enjoy! Smiley

Cheers from Switzerland,
tomm

∴ tomm | 29-Jul-2004 4:34pm est | #5141

sotto wrote:

Thanks! ... thanks to your post i finally found out how i can pass my parameters by reference with call_user_func ... :-)

∴ sotto | 5-Mar-2005 9:15am est | #7132

Perlisbetter wrote:

Not to mention that marrying design elements with logic is
bad for sites where you actually care about design. It means your designers must code around logic, and programmers usually suck at final design, and having different people fighting over the same files is a pain. That's why I use templates for design, and PERL for logic. PERL's better anyway. No qq with PHP is enough for me to not want to use it. Having to escape quotes is a ridiculous and unnecessary pain in the neck.

∴ Perlisbetter | 11-Mar-2005 1:21pm est | #7186

ME wrote:

If you hate PHP what do you use then..and why are you still using PHP?

∴ ME | 30-Mar-2005 1:27am est | #7335

Keith (http://keithdevens.com/) wrote:

Because PHP site == done, while {other language} site == not done. Plus, that language vs. platform thing I mentioned a few comments up. PHP is a crappy language, but a pretty fair platform for making web pages. And PHP 5 is less crappy.

Keith | 30-Mar-2005 1:32am est | http://keithdevens.com/ | #7337

Hendy Irawan (http://dev.gauldong.net/) wrote:

I hate PHP. PHP sucks.

Unfortunately there's no entirely feasible alternative that can replace PHP completely. By "PHP" is also mean "PHP and libraries and applications built on it".

∴ Hendy Irawan | 27-Apr-2005 7:33pm est | http://dev.gauldong.net/ | #7554

Eric Boutin wrote:

I hate PHP too; so many bugs !!! I find one every week. Plus your not even shure your application is going to run on the next php release. That sucks really as soon as I can I move to jsp/j2ee damn php

∴ Eric Boutin | 8-Jun-2005 11:14pm est | #7700

Elliot Anderson wrote:

I found if you want to unshift an array with references, you have to write your own function to take the array, reverse it, do something like $array[] = &$reference; and then reverse again.

∴ Elliot Anderson | 31-Jul-2005 7:25am est | #8031

Mobile developer (http://cellphonesclub.com/wp/) wrote:

I don't hate PHP, but now I battle with parsing of XML where are used rare char-set CP850 (yeh .. "Deutsche Post World Net" still use it). PHP does not support it and PHP does not support Unicode - one more nightmare of PHP ...

∴ Mobile developer | 19-Jun-2006 3:16am est | http://cellphonesclub.com/wp/ | #9519

insta wrote:

Some problems with PHP have nothing to do with the language, but the programmer. Keith, why don't you try checking the INI file for things like how to wrap the source code? You can overwrite values with ini_set. They default to <font, but you can change them to <span

That said, PHP still sucks. Far too easy barrier of entry lets people vomit all over a keyboard and PHP will run it. Then they put on their resume that they're "expert PHP developers", and take my (and other skilled developers') job for half the pay, only to ruin everything a month later with their ineptitude in the language.

Not that I work with anyone like that ...

∴ insta | 7-Jul-2006 9:37am est | #9546

Keith (http://keithdevens.com/) wrote:

Keith, why don't you try checking the INI file for things like how to wrap the source code? You can overwrite values with ini_set. They default to <font, but you can change them to <span

No, you can't. Here's the documentatation on PHP's syntax highlighting: first the ini settings, and next the documentation for highlight_string. It seems that it uses "<span..." in PHP 5 and "<font..." in PHP 4, with no way to change that in either version.

Before "blaming the programmer" (i.e. me) how about you show me in the documentation where it can be changed like you say?

Keith | 7-Jul-2006 11:18am est | http://keithdevens.com/ | #9547

Hannibal wrote:

Is there anything you don't hate? PHP, ASP, ASP.NET? I'm sure you are going to hate RoR too.

You are a hater.

∴ Hannibal | 10-Jul-2006 3:38pm est | #9551

Keith (http://keithdevens.com/) wrote:

I don't hate Python :-P

Keith | 10-Jul-2006 3:46pm est | http://keithdevens.com/ | #9552

Ivo wrote:

Ai, Python. We (two average experienced Linux system administrators) have spent one full working day on trying to get Python to run as a module on Apache. Tweaking some .py file that came with the package finally 'did' it.
PHP is installed and runs correctly in seconds (with good package manager, that is Smiley).

Not flaming Python, just one of my annoyances Smiley

∴ Ivo | 31-Jul-2006 8:08am est | #9579

Michael Goodell (http://peehpee.com) wrote:

Yes PHP is the anti-Christ! Tell your friends! The end is near!

Just a few of my reasons:

1. Post-mortem debugging. Adds hours to finding the simplest bugs. I stopped doing post-mortem debugging in the late 1980's

2. OOP / OOD is an afterthought and does it show!!!

3. Can shut off error reporting! I guess the problem does not exist if you can't see it.

4. Obvious syntax error go unnoticed and Pee-H-Pee thinks all is well. ie foo shoud be $foo and nothing is said.

5. Exception handling? Pee-H-Peez structured exception handling in 5.x is laughable. Nice try boys.

6. It's obvious Pee-H-Pee grew out of a set of PERL scripts.

7. Can scatter settings all over the place. No structure to any applicaitons.

Bottom line: Pee-H-Pee? It's the VB3 for the web.

In other words:

"You can only polish a turd so much before it starts to smear!"

Personally, when the enterprise application calls I break out Java / Servlets / JSP. Apache->Tomcat->FreeBSD!!

Anyone ever gone away from JSP / Java / Servlets to Pee-H-Pee? Anyone who has done so for valid reasons? Not becuase you don't get Java? Anyone? . . . . Didn't think so.....

∴ Michael Goodell | 13-Sep-2006 4:31pm est | http://peehpee.com | #9648

M.Hirsch (http://www.vrdevelopers.de/) wrote:

Another problem related to references that I recently encountered:

I tried to write a (php) replacement for the mysqli functions because my script is supposed to also work without this extension.

90% through writing the wrapper, I noticed that there is no way to replace mysqli_stmt_bind_result()

Variable function arguments don't support references. Smiley frowning

∴ M.Hirsch | 6-Mar-2007 3:31pm est | http://www.vrdevelopers.de/ | #10007

Dave wrote:

Some interesting comments on this blog. Coming from a C++ background I can understand some of the frustrations of dealing with a "hacker language" like PHP, though I think the development community should maybe be a bit more patient with relatively new languages. PHP 5 seems to make a bit more sense, and things like WASP are attempting to drive consistency and proper design/architecture.

Also I don't think PHP is the only language that can be criticised for its "bolting on" of OOP Smiley winking

∴ Dave | 12-Sep-2007 5:24am est | #10296

king game (http://www.kinggame.org) wrote:

If you hate PHP what do you use then..and why are you still using PHP?

∴ king game | 2-Feb-2008 9:25am est | http://www.kinggame.org | #10509

Keith (http://keithdevens.com/) wrote:

Thought I explained this above...

Keith | 3-Feb-2008 8:44pm est | http://keithdevens.com/ | #10510

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

July 2008
SunMonTueWedThuFriSat
 12345
6789101112
13141516171819
20212223242526
2728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 2 posts

Recent comments XML

getElementsByClass function

http://pitfalls.wordpress.com/2008/​07/07/querying-it-jquery-way-getele​ments...

maxgandalf: Jul 7, 5:50am

Generated in about 0.215s.

(Used 8 db queries)

mobile phone