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
2009-01-30 18:48:12
+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.
2009-01-30 18:52:47
ive confirmed this w/ one of the PHP developers
arin sarkissian
2009-02-01 02:17:30
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
2009-03-03 22:33:36
I wish he'd make like a tree, personally.
Ant P.
2009-03-03 23:02:16
-1 PHP never passes by reference if it isn't explicitly told to do so. Objects in PHP 5 are no exception.
nikic
2010-10-17 20:52:53
@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.
2010-10-20 20:07:53
@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
2010-10-21 10:01:14