Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createTag() of AttributesTrait accesses a private attribute without a getter #5

Open
makoru-hikage opened this issue Oct 13, 2016 · 6 comments

Comments

@makoru-hikage
Copy link

Line 129 of AttributesTrait is faulty; it accesses a private attribute. The $attribute property of AttributesTrait used by Tag class is private, but it is accessed with the following:

$tag->attributes = $this->attributes;

The aforementioned line is Line 129 of AttributesTrait.

To summarize what happened: Using the AttributeTrait, class Attributes accessed the private $attributes of Tag class. Although both classes Tag and Attributes use the AttributesTrait that has private $attributes, they cannot access such property without doing a 'getter'.

Will think of a PR soon.
[The usage of PHP Traits is quite confusing, isn't it?]

@donquixote
Copy link
Owner

[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.

@donquixote
Copy link
Owner

Is this not covered by any test, I wonder?

@donquixote
Copy link
Owner

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.

@donquixote
Copy link
Owner

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?

@donquixote
Copy link
Owner

@makoru-hikage
Copy link
Author

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

This solution seems elegant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants