Needs work
Project:
Drupal core
Version:
main
Component:
base system
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
11 May 2018 at 00:00 UTC
Updated:
23 Jun 2023 at 10:42 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
markhalliwellComment #3
markhalliwellWrong component.
Comment #4
markhalliwellComment #5
dawehnerWhile I get the point of it (consistency) I think the better approach is to not expose more array like objects, but rather remove some of them and deprecate the array API, and replace them by proper domain objects.
Comment #6
markhalliwellI understand the end goal.
However, I think this is a necessary intermediate step considering how much [BC] code (that uses arrays) we'll need to support in 8.x.
It'll also give us an idea of what actually still needs to remain as arrays and what can be converted into proper domain objects in the future for 9.x.
That being said, I think some things will always remain an array (like Attachments, Attributes or Variables) as they are simply a collection of things that can be constructed in any number of arbitrary ways. There really isn't any way around that.
This will simply give us a better foundation on how to implement and test them.
It's also a way to help reduce duplication of code for common tasks that we do with these objects in any given area of code.
Suffice it to say, simply saying "we should move away from arrays", while an admirable goal, isn't always appropriate or feasible given the current state of a lot of the current code.
Comment #7
dawehnerSure I get that. The problem though is: when we put out a common base class like this, people will start to use it more and more.
I think duplicate code is less of an issue, especially it is less of an issue when we talk about deprecated code.
Comment #8
markhalliwellI'm fine with marking this as
@internalwith a big note saying something to the effect of "make sure you actually need this before using it, the primary goal in core is to move away from Arrays... etc, etc, etc".Granted that won't exactly help with the "everybody using it" thing, but it certainly would eliminate any responsibility if/when we decide to deprecate/remove it (which I seriously doubt will happen, at least not for a very long time).
The main purpose of this issue is to get something in core that can help standardize a lot of our theme system internals into something that is a little more consistent and easy to work with. Not to mention easy for DX/FX purposes.
We need this baby step.
Comment #9
markhalliwellComment #11
volegerSo what is the next step of this issue? Should we start from the definition of an interface or what?
Comment #12
markhalliwellThis is still semi in the discovery phase as to exactly what is needed.
We'll need to know what array-like objects will need to ensure they can be extended from this base object easily enough.
For now, this is mostly just a placeholder. I'll provide a patch to get this started once I'm confident we're ready to proceed.
Comment #20
kim.pepperI found a need for this in #3292759: Create getters and setters for dynamic Extension properties so wrote up an implementation.
Comment #21
andypostIt looks great and fixes array object for PHP 8.2
Waiting for tests and looks rtbc
Comment #22
kim.pepperI've added get/set methods for more OO property access.
Comment #23
kim.pepperComment #24
smustgrave commentedSeems to have 8.2 failures. Should that be hold up or no?
Comment #25
volegerNo, these were unrelated issues during the test run. The patch contains new additions only.
I suggest using union type argument declarations:
`?string|?int` for `$key` argument
I suggest using a union type argument declaration:
`string|int`
Also, why not introduce the method chaining support for setter methods?
Comment #26
kim.pepperThanks for kicking this issue along!
#25
`?string|?int`is not valid as you can't mix union types with nullable types. Addedstring|int $keyfor get and set methods instead.\ArrayAccessinterface.Added a fluid setter and bumped to 10.1.x
Comment #27
smustgrave commentedChange looks like a good consistentcy addition
Comment #28
catchAre any of the issues that reference this one planning to use it? There's no indication as such in the issue summary. Without a concrete use-case, it won't help with conversions. For example the builder pattern issue provides a class that creates a render array, it doesn't object-ify the render array itself, so it won't help there.
Additionally, if we added something like this, I don't think we should call it ArrayObject because that already exists in SPL.
Comment #29
kim.pepperI came here from #2024043-53: Add Module, Theme, Profile, and Extension value objects where @larowlan suggested this. I think we need a base object that can support the existing arrays that are currently a dumping ground.
Maybe we can call this
BaseArrayObjectto make that more clear?Comment #30
kim.pepperComment #32
donquixote commentedI am skeptical about the use of ArrayAccess objects.
I also don't like these grand base classes, which tend to give their children more capabilities than they need.
E.g. would the extension info classes really need getIterator()?
I would want to see examples how this can improve DX elsewhere - see also my comment in #2024043: Add Module, Theme, Profile, and Extension value objects.
We can create proof-of-concept branches that show how this would be used.
(test cases alone are not sufficient for this)
So +1 to @catch in #28.
I did recently post a proposal involving ArrayAccess in slack, but this was for a very niche and controversial case to allow
$cache[$cid] ??= ..., very different from what we do here.Btw, plain php arrays _do_ have some benefits over general-purpose ArrayAccess objects:
A php array is passed by value, and is therefore in a way "immutable": If you pass it around to other functions, your own copy of the array won't be manipulated. The same cannot be said about mutable ArrayAccess objects.
Also there are operations and functions for arrays that don't work with ArrayAccess objects.
The DX with arrays can be improved with phpstan/psalm advanced types.
I do like value objects, but these work best with a known and limited list of properties.
Or, if we do want to support custom properties in addition to known properties, for the custom ones we can simply have ->get($key) instead of [$key].
This way, when reading the code, we know it is an object.
The only other reason for ArrayAccess might be for BC support, as a smooth replacement for arrays.
But this is already limited if the code we want to support uses array type hints, or things like preg_grep() etc.