PHP array and object addition and string indexing

While reading someone’s code, I came across the following sort of thing:


function foo($config = array()) {

$this->_config += $config;
//...
}

To which I thought WTF? How does PHP cast an array to a number to perform addition?

A few random tests later, it appears PHP joins the two arrays together, only adding indexes that appear in the second (and not first) array.

(as per php manual – array operators)

Namely :

$array_1 = array(‘a’,’b’, 4=>’c’);
$array_2 = array(4=>’e’, ‘f’, ‘g’);

$array_1 += $array_2;

print_r($array_1);

will give :
Array
(
[0] => a
[1] => b
[4] => c
[5] => f
[6] => g
)

(Note; 4=>e is not in the resultant array, because 4=>c was in array_1).

After a round about conversation with Moobert on this on IRC – at which point he probably put me out of my misery, he also gave me the following :

$x = ‘test’;
echo $x[‘whatever’]; // outputs ‘t’

Which I can understand – as PHP allows for per-character access to a string (based on position) – hence ‘something’ gets casted to zero, and we get the first character back.

I know I can cast objects to arrays, and vice versa.

Seemingly Objects don’t follow any sort of ‘union’ rule however… as adding two objects results in ‘2’. Not sure how PHP converts an object into ‘1’… but there we are. I sort of expected to get an object back (assuming the inputs were the same type) with a union of properties, but no.

Twitter Weekly Updates for 2009-10-25

  • Was there enough Lego at #lrl2009 ? #
  • Bye bye #lrl2009 till next year perhaps ? #
  • Windows 7? Is that like ubuntu but non-free? #lrl2009 #
  • #lrl2009 where we play with Lego and the ninky nonk #
  • The wheels on the bus go round and round, round and round, round and round…. All day long. #
  • Fun times. Waiting for train at euston. #
  • Must not drop phone in bath… #
  • Must start using phpunit double/stubbing stuff. Shame the docs seem crap for it. Can has api? #phpunit #
  • WTF Is ironing water for m&s?? #
  • I am a chocolate biscuit pig #
  • Great. Runny nose, stupid throat and my body feels tired. Just what I do not want. Go away germs. #
  • Slug and lettuce dessert. Hopefully no slugs involved. Just dead apples and custard. #
  • Looking forward to ben&jerrys ice cream later … Shame mr toddler doesn't like it ….. *ahem* #

La Leche League Conference 2009

Breastfeeding extravaganza, well perhaps…. La Leche League conference 2009 in Alfreton

For Friday/Saturday, Kat and I went up north to the annual La Leche League conference. For those of you wondering – the La Leche League is all about breast feeding and any related child raising stuff. Suffice to say all the attendees were middle class, carrying their baby/toddler in a sling and touting Onya bags. I suspect a large proportion of the children there were also home schooled, judging from the few kids I overheard talking about it.

Kat went to various talks about attachment parenting, how milk is made and so on. One of the talks had something about a child being thrown out of the window to be caught by their father down below. An interesting idea, but I fear it might lead to social services visiting.Although I suspect I’m already going down this slippery slope as Rowan is starting to demand I throw him up in the air (“Jump! Up!”)

Rowan and I spent pretty much the entire time in the onsite soft-play room – he found plenty of balls to play with, and I wasted time on my iPhone eating chocolate biscuits. Unfortunately there wasn’t any wireless available :-/

I did go to one talk, helpfully titled ‘Dads’ which saw a few fellow soft-play inmates/fathers attend. It was a vaguely interesting talk, not surprisingly focussing on the role Dad’s have in breastfeeding. I think my attitude to breast feeding can be briefly summarised as :

  • Why would I be interested in bottle feeding? This would mean I’d have to do something in the night/day. I want Kat to do all of this. The stupid argument over the man feeling involved is rubbish – if I want to be involved with Rowan I could easily do something with him before/afterwards. Yes, I tend to get lumped with nappy changing when I’m around, but it’s not something that bothers me – it’s just something that needs doing.
  • Bottles are hassle – cleaning + sterlisation + worrying about whether you’ve given the baby enough/too much – no thanks.
  • Environmentally, breast milk is hard to beat. Formula is neither free or sterile.
  • From a health point of view, it’s good for my wife, and also for the baby. Apparently it makes the child more intelligent, less likely to be obese and so on. The longer they are breastfed the better all round.

The Dad’s talk also covered things like – “how to best support your breastfeeding wife” and so on. Someone raised the point of needing to be able to defend your wife/baby if challenged in public – personally I think this is a non-issue, as before I’d be able to open my mouth, Kat would be most of the way through her fifth or sixth argument as to why it’s none of their business or how it’s the natural thing to do for baby/toddler etc. Rowan has also probably been through a secret army special ops training course to bite any such people, given the correct key-word by Kat. Unfortunately for Kat, she’s been waiting 21 months for someone to challenge her over it, and it’s not happened yet. I hope they give me some warning they’re about to argue with her – so I can video it.

What was interesting was how some of the mothers there were still feeding 5-7 year old children. We’ve not really thought that far ahead yet – Rowan still isn’t two. He is now night weaned (and thankfully sleeping from around 8pm to 5am straight through), but still demands feeds in the morning and evening at the very least. If he’s somewhere strange then he’ll ask at random points throughout the day as well. But then, he won’t drink cows milk (not that I can blame him).

There wasn’t much, in the way of shops, in Alfreton – however it seemed to be doing far better than Bromsgrove as far as town centres go – and it had a big f*** off Tesco in the middle of it too – but still had two bakeries, a Curry’s and various other little shops.

Twitter Weekly Updates for 2009-10-18

  • Wondering what it means if a shop window mannequin has plasters on her wrists…. #
  • Perhaps I now know what it's like for my wife when I go mountain biking or something for a day and she's left stranded in some strange town. #
  • Final day of le leche league conference (breast feeding etc etc). Here's to hoping a day in soft play doesn't kill my brain #
  • Just saw a van take out a cyclist. He seems alright thankfully. Bike + windscreen didn't fare too well though. #
  • I wonder how long a ball pit and garage will keep rowan occupied? #
  • At le leche league conference #lll #alfreton #breastfeeding #
  • Discovers /* lol version of getHasProfile() */ public function canHasProfile() { return $this->getHasProfile() } #php #lolcat #codehumor #
  • I'm fleeing London…. So long suckers. Till wednesday next week anyway…. #
  • First BBC php zend framework course nearly over. Stupid Internet connection causing grief :/ #
  • I have difficulty in believing m&s's green plan b crap when it feels like I'm roasted by a heater above the door on entry. #
  • Train (hopefully) hurtling towards London; three days of tension await. GnR's Estranged seems to be helping. #ihatemonday #
  • RT @codinghorror Some people, when confronted with a discussion problem, think "I know, I'll use threads!" Now they have two problems. #
  • Looking forward to sleep…. #

Twitter Weekly Updates for 2009-10-11

  • Kidderminster's sainsburys customers at least don't look like zombies…. Manchester's asda ones definately did yesterday #
  • Perhaps it's Related to my coke (a-cola) binge? #
  • What maniac has replaced my bladder with a womans???? #
  • Listening to v loud music to stay awake while driving :-/ #
  • Toddler vomit… Hmm #
  • guess i won't be making the half marathon tomorrow thanks to my evil sister in law tempting me with beer and keeping me up till midnight :-/ #
  • Here's hoping we miss the rioters. #phpnw09 #Manchester #
  • Hmm expression web 3 superpreview looks perhaps useful. #phpnw09 wish they'd talked about this before the end #Microsoft #browser #ie #
  • Wish this Microsoft sales pitch would end. At least the ms presenter hates ie6. #phpnw09 #
  • "We never turn people away unless it's a retarded idea." Thx @scottmac. #phpnw09 (re contributions to php.net etc) #
  • Plenty of jokes from @scottmac (ming(e)) etc. Good fun so far! #phpnw09 in reply to scottmac #
  • Xdebug talk excellent. Thanks @derickr #phpnw09 #
  • RT @skoop the #phpnw09 lunch was made of pure WIN! awesome work @phpnw ditto. Great food. #
  • Scaling web pages … Good start .. #phpnw09 #
  • Don't all developers use a vcs now ? #phpnw09 #
  • Excellent first talk – thanks @kevlinhenney #phpnw09 #
  • The moral dilemma of eating baby killing nestle chocolate …. When given it in a goodie bag. Hmm #phpnw09 #
  • The journey starts #phpnw09 #
  • is lusting after a cup of hot chocolate ( it feels cold today ) #
  • RT @sil Oh. My. God. http://bit.ly/WMfLf #fb (the pain!) #
  • Cake Friday …. Again. #
  • Toddler slept well (through to 8pm to 6:30 ish) …. Coincidence that I put him to sleep ? #
  • Clearly I'm showing my age by listening to proper guns and roses and it reminding me of school 14 years ago #
  • Death^h^h fun fair slowly arriving in bromsgrove #
  • The sound of rain seems so almost strange. It's been dry for ages it seems. #
  • Tonight's experiment – late dinner and toddler appetite – is later better? #
  • Clearly not moving any time soon. Everyone walking around on the road or going for a wee. Should've left the road 400metres back at banbury #
  • Stupid m40 northbound (after babury) brokeness. #

Rowan!

Rowan’s talking is finally progressing quite well – I think he’s finally figured out that he has to talk to us if he wants “stuff” (more please; please more) and he’s beginning to use more syllabals and string words together (piece of toast, hello daddy, bye daddy). There are loads of sounds he can’t (seemingly) make yet – so some words miss the first letter or two (e.g. nny for Jenny).

He’s also starting to push other kids over; [un]fortunately he’s relatively big compared to many of his peers – so there isn’t much chance of someone pushing him over in return.

Holiday pour moi?

We seem to be contemplating the idea of going abroad over Christmas – it’s always a slightly stressful time of the year, and it also coincides with about the only quiet time of the year I get (with Pale Purple). And I’ve sort of promised Kat a holiday in return for putting up with quite a busy October/November.

Unfortunately going anywhere involves air travel, which is sort of against Katherine’s eco-push.

So, we may not be here over Christmas. Perhaps my stingy side will kick in and rebel against the fact prices appear to double for the Christmas week (or halve if we leave it to the middle of January).

I couldn’t face the prospect of seeing Butlins again … so it has to be somewhere different if we go anywhere.

Perhaps we should just visit somewhere like Canada/America instead. Not that I have any idea of the costs involved.

Living Rainforest

On Sunday, while Kat was at some organic gardening thing, Rowan and I went to the Living Rainforest (which is vaguely near J13 of the M4). Due to a failure to read on my behalf, we first headed towards RG10 0TN using the GPS, discovered this clearly wasn’t the right place and then headed to RG18 0TN…. only 30 something miles away. Thankfully Rowan was good natured and the day wasn’t a total loss.

The living rainforest was a lot smaller than I’d expected, but Rowan found some mud to play with and door way flaps to keep running through, so he was quite happy.

Some pictures are here

On the way home, the stupid M40 was somewhat broken, and what should have been an hour and a half journey turned into 3 hours+.