KBD

Keith Devens .com

Saturday, February 5, 2005 Flag waving
← 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.

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

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

Rules:

  • No HTML Allowed
  • Formatting: **bold**, *italic*, _underlined_, --strikeout-- (and they can be combined)
  • Links: "text"="url" means <a href="url">text</a>, though URLs are auto-highlighted
  • Blockquote: begin your paragraph with a > (greater-than sign)
  • Lists: paragraphs begin with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{newline}...{newline}?!/code

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

: (optional)
:

February 2005
SunMonTueWedThuFriSat
 12345
6789101112
13141516171819
20212223242526
2728 



RSS feed RSS feed for Keith's Weblog
Weblog archive
Recent comments
  on 14 posts
Blogroll

Countdowns

  1. 86 days until classes end
  2. 95 days until finals end
  3. 237 days until Serenity opens

Must-read sites

Recent comments XML

newSuper Mario Brothers 3... in 11 minutes

http://bisqwit.iki.fi/nesvideos/WhyAndHo w.html

Yeah......

Hank: Feb 5, 4:51pm

Johnny Walker Blue Label

>Sorry, I didn't read the rules before my first post, and no, I haven't bee...

Tom SC: Feb 4, 11:46am

Storing times in MySQL

I'm hosting mysql outside of my country and my question how do I store date...

161.142.148.211: Feb 4, 4:47am

I can't believe my site was down *again*

I've seen that chess thing before. It's very neat, though unfortunately it ...

Keith: Feb 3, 9:53pm

Our Oldest Enemy: A History of America's Disastrous Relationship with France

I don't know why you've made too much noise for nothing, and be agressiv. W...

84.100.5.225: Feb 3, 6:45pm

Power Line: Live-Blogging the State of the Union

How frustrating. I hate it when people just post links to things without of...

Keith: Feb 3, 6:20pm

Enterprise cancelled

That sucks. And it was just getting really good with Manny Coto at the helm...

Keith Gaughan: Feb 3, 10:05am

Slashdot | DARPA Contracts For AI Technology

So you're familiar with their research? I was wondering if you would be. Ha...

Keith: Feb 2, 7:19pm

Energy drink review sites

If you want a real popular energy drink (#2 Worldwide, #1 in Mexico) try Bo...

Andrew Gerhardt: Feb 2, 4:00pm

Caroline Dhavernas on WGN

Oh man, Wonderfalls is now #4 on Amazon's DVD list. MacGuyver has, unfortun...

Keith: Feb 1, 10:06pm

Kerry and the Sandinistas

Well, you might be onto something. I just think that the SDI program appear...

Elling: Feb 1, 5:18pm

User-mode Linux

well id like to change my mind about redwoodvirtual, true they had a stage ...

Stephen: Jan 31, 7:20pm

The suicide bombers don't count!

Of course I think it's relevent. But I don't think it needs to be explicit...

Jim: Jan 31, 4:46pm

Maps of Iraq

My brother is at Camp Wolf as well. He told his girlfriend that he feels s...

Jessica: Jan 30, 10:11pm

Blogroll

powered by blo.gs

Little Green Footballs
42 minutes
Ned Batchelder
1 hour, 43 minutes
Roger L. Simon
2 hours, 30 minutes
The Smallest Minority
2 hours, 51 minutes
Instapundit.com
2 hours, 52 minutes
Country Store
3 hours, 33 minutes
Joseph Scott's Blog
4 hours, 4 minutes
IRAQ THE MODEL
4 hours, 47 minutes
Power Line
4 hours, 54 minutes
A Constrained Vision
5 hours, 11 minutes
TinyApps.Org
5 hours, 27 minutes
manalang.com
8 hours, 14 minutes
Jeremy Zawodny's blog
8 hours, 47 minutes
Legal Theory Blog
10 hours, 23 minutes
Keith's Weblog
18 hours, 45 minutes
alexking.org: Blog
20 hours, 19 minutes
Hack the Planet
21 hours, 8 minutes
Photo Matt
22 hours, 13 minutes
WHEDONesque
23 hours, 14 minutes
Right Wing News
23 hours, 30 minutes
Daily Python-URL! (from the Secret Labs)
1 day, 2 hours
One Hand Clapping
1 day, 3 hours
Lifehacker
1 day, 8 hours
Talideon.com Linklog
1 day, 9 hours
ongoing
1 day, 19 hours
Cox & Forkum
2 days, 3 hours
The Farm: The Tucows Developers' Hangout
2 days, 4 hours
Civilian Gun Self-Defense Blog
2 days, 6 hours
paranoidfish
2 days, 7 hours
why the lucky stiff
2 days, 7 hours
Sam Ruby
2 days, 8 hours
ideoplastos
2 days, 10 hours
Lambda the Ultimate - Programming Languages Weblog
2 days, 11 hours
Loud Thinking
2 days, 16 hours
Amy Ridenour's National Center Blog
3 days
Ping-O-Matic!
3 days, 3 hours
Clayton Cramer's BLOG
3 days, 4 hours
Second p0st
3 days, 7 hours
Simplelinks
3 days, 9 hours
Wunderkinder
3 days, 10 hours
Efectos Especiales
3 days, 23 hours
The Amazin Asian
4 days, 7 hours
Imperialviolet
5 days, 6 hours
American RealPolitik
6 days, 11 hours
vsbabu.org
1 week
Pinging Knight
1 week
Sender Traumwind
1 week, 1 day
Jeff Moore's Blog
1 week, 1 day
Dunstan's blog
1 week, 3 days
Simon Willison's Weblog
1 week, 4 days
blog.se
1 week, 5 days
inluminent
2 weeks
only this, and nothing more
2 weeks, 3 days
Internet Alchemy
2 weeks, 3 days
Keith's Inklings
2 weeks, 5 days
The Fishbowl
2 weeks, 6 days
Kayodeok's Weblog
3 weeks, 4 days
Armed and Dangerous
2 months
Ian Bicking
2 months, 2 weeks
The Daily WTF
2 months, 2 weeks
The PHP WTF
3 months
Squawks of the Parrot
3 months

This page took about 0.231 seconds to generate.