From: Andy Armstrong Date: 15:52 on 11 Jan 2007 Subject: PHP sucks dick through a straw 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 (reposted from london.pm) -- Andy Armstrong, hexten.net
From: Andy Armstrong Date: 17:13 on 11 Jan 2007 Subject: Re: PHP sucks dick through a straw On 11 Jan 2007, at 15:52, Andy Armstrong wrote: > "Complex arrays are sometimes rather copied than referenced. Thus > following example will not work as expected." [1] Although in fact this does work on both PHP 4.4.4 and PHP 5.2.0. Way to go with the alarmist documentation which means I still don't know whether it's right or just happens to work. In fact if there's one thing I hate more than PHP itself right now it's the fact that its online documentation is rammed to the gills with cargo-culted half truths, suspicions and downright rubbish. <?php $list = array( '1' => array( 'name' => 'Frib', 'parent_id' => '3' ), '2' => array( 'name' => 'Frob', 'parent_id' => '3' ), '3' => array( 'name' => 'Top 1', 'parent_id' => null ), '4' => array( 'name' => 'Top 2', 'parent_id' => null ), '5' => array( 'name' => 'Frub', 'parent_id' => '4' ), '6' => array( 'name' => 'Freb', 'parent_id' => '4' ) ); $root = array(); foreach ($list as $id => $obj) { if (is_null($obj['parent_id'])) { $root[] =& $list[$id]; } else { $list[$obj['parent_id']]['children'][] =& $list[$id]; } } print_r($root); ?>
Generated at 10:28 on 16 Apr 2008 by mariachi