PHP sucks dick through a straw

Andy Armstrong andy at hexten.net
Thu Jan 11 15:11:24 GMT 2007


Humph. I'm writing PHP (again) today. I'm just doing a bit of munging on
a data structure - basically turning a hash of objects some of which
have another object in the same hash as their parent into a tree
containing the same objects. It'd be trivial in Perl. In PHP it's not
quite so simple. PHP really likes every assignment to be a deep copy. In
mitigation it dangles the carrot of pretending to support references
using the =& assignment operator. Except that:

    "Complex arrays are sometimes rather copied than referenced. Thus
     following example will not work as expected. [1]"

     <?php
         $top = array(
             'A' => array(),
             'B' => array(
                 'B_b' => array(),
             ),
         );

         $top['A']['parent'] = &$top;
         $top['B']['parent'] = &$top;
         $top['B']['B_b']['data'] = 'test';
         print_r($top['A']['parent']['B']['B_b']); // array()
     ?>

There's plenty of madness elsewhere on the same page too. In PHP4
something as innocuous as

     $obj = new Object();

actually makes a deep copy of the newly created object! So the idiom
is instead

     $obj =& new Object();

Which is OK once you get used to the ugliness - but

    "Since PHP 5, new return(sic) reference automatically so using =& in
     this context is deprecated and produces E_STRICT level message."

So it's impossible to write code for something as simple as creating a
new object that works right in PHP4 and still works without warnings in
PHP5. Which of course means you give up on using E_STRICT, which means
you don't see all the other shite that's going on - some of which might
actually be informative.

Somebody remind me why do so many people use a language that can't even
get this completely basic stuff right.

[1] http://uk.php.net/manual/en/language.references.whatdo.php

--
Andy Armstrong, hexten.net



More information about the london.pm mailing list