KBD

Keith Devens .com

Saturday, July 4, 2009 Flag waving
Есть Два Цвета: Черный И Белый, А Есть Оттенки, Которых Больше. ('There are two colors: black and white. Others are just shades.') – unknown
← Evaluation: moving from Java to Ruby on Rails for the CenterNet rewriteThanksgiving →

Daily link icon Thursday, November 24, 2005

PHP sucks yet again

Via Jeff, just found these PHP Developers' Meeting minutes on PHP 6. Call-time pass-by-reference was deprecated for no reason somewhere along the line. Now they're un-deprecating it, changing it back from an E_WARNING (they claim it's E_NOTICE, but it's not) to just E_STRICT (though they're also turning E_STRICT on by default):

The first issue that we raised was changing the E_NOTICE error for call-time-pass-by-reference to an E_ERROR, or simply throwing a parse error. We argued over this case and we decided to change this E_NOTICE to an E_STRICT instead as it was argued that there is nothing wrong with doing a call-time pass by reference.

Argh! I vented my frutration about this feature being deprecated since it broke some of PHP's built-in functionality. Now it's just "Sorry, there really was no reason for us to deprecate that feature. Our bad".

On the bright side, it looks like they're removing magic quotes and register globals.

Also, the PHP people couldn't agree on how to fix a sore spot in the language if you develop with notices on. You commonly have to do things like the following:

<?php
$foo 
= isset($_REQUEST['foo']) ? $_REQUEST['foo'] : NULL;
?>

so that you don't get an error if $_REQUEST['foo'] doesn't exist. Obviously that's really cumbersome to do over and over again. They proposed adding an ifsetor function that does essentially the following:

<?php
function ifsetor($var$default){
    return isset(
$var) ? $var $default;
}
?>

But note the special casing in their example:

<?php
$foo 
ifsetor($_GET['foo'], 42);
?>

That's not allowed if $_GET['foo'] doesn't already exist; if I call my version of ifsetor I get a NOTICE error. So, they'd add a special case in the language (just like they already have with isset) to make it appear as if $_GET['foo'] returns NULL if it's not defined. But if that was the case it would defeat the whole purpose of the feature anyway!

However, they decided against ifsetor and instead are adding a syntax extension that lets you elide the middle value for the ternary operator for the common case as shown above, which would allow:

<?php
$foo 
$_REQUEST['foo'] ?: NULL;
?>

Which really isn't bad at all. Only:

Instead of implementing ifsetor() we remove the requirement for the "middle" parameter to the ?: operator. The middle parameter then defaults to the first one. If the first parameter is not set, then we will still throw an E_NOTICE.

So, again the feature becomes useless for the common case I've been talking about. However, they've provided the following for me:

<?php
$foo 
input_filter_get(GET'foo'FL_INT) ?: 42;
?>

Great.

FYI, I have a few convenience functions just for these common cases:

<?php
function g(&$var$key$default NULL){ #make it easy to develop with notices on
    
return isset($var[$key]) ? $var[$key] : $default;
}
function 
r($key){ return g($_REQUEST$key); }
?>

g optionally does some more than that, but that's the base case.

Now, this I'm very happy about. They're getting rid of the `{}` indexing for strings (which they admit was a hack to simplify their parser) and allowing slicing notation like Python does. That's excellent. They k'd on named parameters, however.

Note: any emphasis in any quote above is my own.

← Evaluation: moving from Java to Ruby on Rails for the CenterNet rewriteThanksgiving →

Comments XML gif

Dennis Pallett (http://www.phpit.net) wrote:

I hear ya, it's a pain in the ass to get notices for every unset GET or POST variable, and it's exactly the reason why I usually turn off notices, even during developing (which I shouldn't do).

I like your functions though, and I'll probably use them from now on.

∴ Dennis Pallett | 24-Nov-2005 3:39pm est | http://www.phpit.net | #8732

Dennis Pallett (http://www.phpit.net) wrote:

Oh, and a real shame about named parameters. I love those, and usually use parameters now, i.e.

function foo ($params) {

echo $params['userid'];
echo $params['pass'];
}

instead of:

function foo ($user, $pass)
∴ Dennis Pallett | 24-Nov-2005 3:41pm est | http://www.phpit.net | #8733

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

Here's the whole g function, for your entertainment (update, Dec 7: fixed bug in g):

<?php
function g(&$var$key$default NULL){ #make it easy to develop with notices on
    
if(!is_array($key)){
        return isset(
$var[$key]) ? $var[$key] : $default;
    }else{
        
$result = array();
        if(isset(
$key[0])) #numeric
            
foreach($key as $k)
                
$result[] = g($var$k);
        else 
#associative
            
foreach($key as $k=>$v)
                
$result[$v] = g($var$k);

        return 
$result;
    }
}
?>

So, if $key is an array you get an array back composed only of the keys you specify in the array, where keys that don't exist get NULL. And if you specify an associative array you can transform the keys in an array from the keys in that array to the corresponding values.

I also use this function everywhere:

<?php
function e($str){ echo htmlspecialchars($str); }
?>

It's way easier to type e($foo) than echo htmlspecialchars($foo) all over the place. I have a lot of these little functions I use everywhere on my PHP page, if you're interested.

Keith | 24-Nov-2005 6:13pm est | http://keithdevens.com/ | #8734

Keith Gaughan (http://talideon.com/) wrote:

I'll show you something moronic. I've a function similar to your e() function that I use called eecho(). The following code doesn't work:

<?php
function eecho() {
    echo 
htmlentities(implode(''func_get_args()));
}
?>

But this does:

<?php
function eecho() {
    
$args func_get_args();
    echo 
htmlentities(implode(''$args));
}
?>

Now, that is utterly stupid.

∴ Keith Gaughan | 25-Nov-2005 8:33am est | http://talideon.com/ | #8737

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

That's weird. I'll have to try that later and see why it doesn't work. Another thing is, PHP's parser is too lame to allow things like:

<?php
$foo 
"bar baz";
echo 
explode(' '$foo)[0]; #should be 'bar', but gives parse error
?>
Keith | 25-Nov-2005 12:26pm est | http://keithdevens.com/ | #8740

M. Bean wrote:

This post is why your blog software needs a <--more--> feature to paginate for you.

∴ M. Bean | 26-Nov-2005 5:48am est | #8745

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

You mean, because this post was too long for the whole thing to show comfortably on the home page? You're saying you'd like something like what Sam Ruby does?

Keith | 27-Nov-2005 6:09am est | http://keithdevens.com/ | #8751

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 2009
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
262728293031 



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

Recent comments XML

new⇒Wizard's First Rule

> while it is cheesy to some​extent, I actually found it to be​pretty enjoy...

Keith: Jul 3, 6:33pm

I hate Norton Antivirus

I bought Norton 2009 and it is not​installing on my computer!!!
It​seems l...

o'neil: Jun 30, 11:44am

Generated in about 0.884s.

(Used 8 db queries)