-
Notifications
You must be signed in to change notification settings - Fork 11
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
Comments
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. |
Is this not covered by any test, I wonder? |
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. 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 One difficulty with static method constructors is always that they need to call |
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? |
"Or add this as an optional parameter to Tag::__construct()" This solution seems elegant. |
Ok here is a simpler php example. trait T {
public function __construct(private int $x = 5) {}
}
class C {
use T;
public function foo() {
$d = new D();
$d->x = 5;
}
}
class D {
use T;
}
(new C)->foo(); Indeed we get "Fatal error: Uncaught Error: Cannot access private property D::$x in /in/0HNFH:11" It seems within this library the |
Line 129 of
AttributesTrait
is faulty; it accesses aprivate
attribute. The$attribute
property ofAttributesTrait
used byTag
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
, classAttributes
accessed theprivate $attributes
ofTag
class. Although both classesTag
andAttributes
use theAttributesTrait
that hasprivate $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?]
The text was updated successfully, but these errors were encountered: