Coder Social home page Coder Social logo

Still mutable about carbon HOT 13 CLOSED

beberlei avatar beberlei commented on May 28, 2024 3
Still mutable

from carbon.

Comments (13)

amitailanciano avatar amitailanciano commented on May 28, 2024 8

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

from carbon.

marijn avatar marijn commented on May 28, 2024 2

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

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

    $instance->modify($mod);

    return $instance;
}

from carbon.

briannesbitt avatar briannesbitt commented on May 28, 2024

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.

from carbon.

marijn avatar marijn commented on May 28, 2024

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

Please consider making this immutable.

from carbon.

briannesbitt avatar briannesbitt commented on May 28, 2024

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

from carbon.

briannesbitt avatar briannesbitt commented on May 28, 2024

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

from carbon.

rjkip avatar rjkip commented on May 28, 2024

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?

from carbon.

rjkip avatar rjkip commented on May 28, 2024

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

from carbon.

briannesbitt avatar briannesbitt commented on May 28, 2024

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

from carbon.

falk avatar falk commented on May 28, 2024

Someone tried to fix it in php 5.5: http://www.php.net/manual/en/class.datetimeimmutable.php , but not without issues http://comments.gmane.org/gmane.comp.php.devel/78803

from carbon.

briannesbitt avatar briannesbitt commented on May 28, 2024

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

from carbon.

beberlei avatar beberlei commented on May 28, 2024

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

from carbon.

tabennett avatar tabennett commented on May 28, 2024

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

from carbon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.