Carbon: Still mutable

Created on 12 Sep 2012  路  13Comments  路  Source: briannesbitt/Carbon

The most annoying thing about PHP DateTime is that they objects are mutable. It seems this is not changed in Carbon?

Most helpful comment

+1, would love to see this -- remembering to chain ->copy() all the time is pretty annoying/frustrating

All 13 comments

Correct. There are no immediate plans to change this. However, I don't think it would take too much to create a subclass that used a copy on write strategy for mutating function calls.

I agree with @beberlei, the fact that DateTime is mutable is very annoying.

Please consider making this immutable.

So would a copy on write implementation fit the bill? Thoughts?

Anything that modifies the internal state should return a new instance.

<?php
public function modify($mod)
{
    $instance = clone $this;

    $instance->modify($mod);

    return $instance;
}

Yes, I get that.. hence the copy on write implementation comment. Is that what you guys are looking for?

I agree, this behaviour is annoying, but wouldn't it break BC? What about a copy() method to allow chaining?

<?php

# Hmm, no pun intended here...
$carbon->copy()->modify("midnight");

Perhaps the copy on write implementation should wait for a BC breaking v2.0.0?

Meh, never mind. It already exists... :x

No further action here... going to close it for now.

Its not high on my list right now. As I have said though, I think a copy on write implementation would be pretty quick.

@falk the issues of the mailing list were completly fixed, 5.5 has an immutable date time that works flawlessly.

+1, would love to see this -- remembering to chain ->copy() all the time is pretty annoying/frustrating

+1, this just bit us today at work. Side-effects suck and make Carbon more difficult to use as a value object.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kylekatarnls picture kylekatarnls  路  5Comments

joomartin picture joomartin  路  4Comments

scs-ben picture scs-ben  路  3Comments

yica00 picture yica00  路  4Comments

ssi-anik picture ssi-anik  路  4Comments