With the new Entity field API, we've to unset all defined entity properties for the magic getters to apply. #1778178: Convert comments to the new Entity Field API moves that to a central init() function, but still it's ugly. So let's discuss how to improve things here.
One think das-peter came up with in #1818556: Convert nodes to the new Entity Field API was to use get_class_vars(get_class($this)); automatically unset all of them. Maybe we need a function helper so the code runs outside the entity scope and does not include protected properties. We could also use reflection, but I don't know whether that's performing well.
Another possibility would be to remove the defined properties, however I think they are a good DX improvement. A downside of them right now, is that right now we've entity field definitions that kind of duplicate the docs of the entity class properties. We could look into using annotations to parse entity base-field definitions out from the entity class property docs though.
Comments
Comment #1
fagoadding tag
Comment #2
das-peter commentedAs far as I know the most IDE's unfortunately don't support the
@propertyannotation introduced in phpDoc years ago.So for good DX I don't think we can't get fully rid of the properties. :|
get_class_vars(get_class($this));could be a solution, but as fago said we've to be careful because of the scope we use this. A helper function outside the class could work but seems a bit ugly.If it is always true that properties that can be unset have a return from
$this->getPropertyDefinition($property)we could use this as condition for unset properties returned byget_class_vars(get_class($this));. I think the patch I've posted in #1818556: Convert nodes to the new Entity Field API already uses this construct.Reflection sounds interesting, but definitely would need a benchmark ;)
Comment #3
fagoYes, just looping over property definitions would be the simplest fix and should totally work. I'm not sure it's feasible from a performance perspective though... Also, it feelds wrong to depend on that for being able to construct or unserialize the object, so I'd prefer to avoid that if possible.
Comment #4
das-peter commentedI've just figured out that,
call_user_func('get_class_vars', get_class($this))resets the scope the function is called in. That way we should be able to fetch just the public properties of a class within a method of the class itself.Comment #5
das-peter commentedI did some benchmarks to compare using plain
unset()with dynamic approaches.It looks like a generic approach takes up to 3 times longer - but it heavily depends on the number of the calls.
In absolute numbers this means for 100000 calls that the fastest dynamic approach takes 0.4842 seconds longer (260%) than the usage of
unset().The question now is, if this gain of DX is worth this :)
Used code:
Comment #6
berdirClosing as duplicate of #2095919: Kill ContentEntityBase::init() , we don't need them anymore as we have interfaces & methods now, just need to get rid of the remaining ones :)