views:

454

answers:

2

Or: Should I optimize my string-operations in PHP? I tried to ask PHP's manual about it, but I didn't get any hints to anything.

A: 

A quick google would seem to suggest that they are mutable, but the preferred practice is to treat them as immutable.

inkedmn
+8  A: 

PHP already optimises it - variables are assigned using copy-on-write, and objects are passed by reference. In PHP 4 it doesn't, but nobody should be using PHP 4 for new code anyway.

Ant P.
ive confirmed this w/ one of the PHP developers
arin sarkissian
I really wish Rich B would stop going around converting valid (British) English spelling into US English... Not all of us are from the US.
James Burgess
I wish he'd make like a tree, personally.
Ant P.
-1 PHP never passes by reference if it isn't explicitly told to do so. Objects in PHP 5 are no exception.
nikic
@nikic: it took zero effort to look this up on the PHP site - “PHP treats objects in the same way as references or handles, meaning that each variable contains an object reference rather than a copy of the entire object.”
Ant P.
@Ant P: The manual entry is ambiguous. What they want to say is that objects aren't stored in an variable that is being passed around, but that a pointer to the actual data is passed around. But still this pointer is passed by value, not by reference. Easy example that shows that: `function f($obj) { $obj = 'foo'; } $obj = new stdClass; f($obj); var_dump($obj);`. If `$obj` were passed by reference it would print `'foo'`, not `'stdClass'` ;)
nikic