Code Monkey home page Code Monkey logo

Comments (6)

donquixote avatar donquixote commented on June 7, 2024 1

Currently Tag / AttributesTrait are designed as immutable, so all the setters create copies. Nowadays I call such setters not setSomething(), but withSomething(), or withAddedSomething(). E.g. $car->withColor('blue').

Calling such methods always has a tiny little overhead.

Within a constructor or when an object is being freshly created, the immutable aspect is not really needed. I would prefer to not create object copies for no reason.

Maybe we should have a static method Tag::createWithAttributes($tagName, $attributes) ? Or add this as an optional parameter to Tag::__construct()?

One difficulty with static method constructors is always that they need to call new static or new self, so they might break in a subclass, if the subclass has a different constructor signature. I already kinda dislike AttributesTrait::create() for this reason.

from cellbrush.

donquixote avatar donquixote commented on June 7, 2024

[The usage of PHP Traits is quite confusing, isn't it?]

Yes, I use them a lot less nowadays. They do not properly encapsulate anything, and make it really hard for an IDE to figure out what's going on. I wish PHP had real multiple inheritance.

from cellbrush.

donquixote avatar donquixote commented on June 7, 2024

Is this not covered by any test, I wonder?

from cellbrush.

donquixote avatar donquixote commented on June 7, 2024

Or maybe a good design for immutable classes is to have a base class with protected mutable setters, and then a subclass which is immutable because it only calls those setters during construction?

from cellbrush.

donquixote avatar donquixote commented on June 7, 2024

https://twitter.com/brain4lunch/status/786526053640482817 :)

from cellbrush.

makoru-hikage avatar makoru-hikage commented on June 7, 2024

"Or add this as an optional parameter to Tag::__construct()"

This solution seems elegant.

from cellbrush.

Related Issues (9)

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.