ArrayObject
ArrayObject extends IteratorAggregate implements ArrayAccess, Serializable, Countable
Wraps an array into an object container (array access, counting, iteration, serialization), subclassable for extra behavior.
C C23
#include <stdio.h>
// C in reverse: arrays are raw memory — object-ness comes from a hand-written struct + functions
typedef struct {
int items[8];
int count;
} List;
static void list_add(List *l, int v) { l->items[l->count++] = v; }
int main(void) {
List l = {{0}, 0};
list_add(&l, 1);
list_add(&l, 2);
list_add(&l, 3);
l.items[0] = 9;
printf("%d [%d, %d, %d] %d\n", l.count, l.items[0], l.items[1], l.items[2], l.items[0]);
return 0;
}
3 [9, 2, 3] 9
C++ C++26
#include <iostream>
#include <vector>
int main() {
// C++ in reverse: std::vector is already an object-like array (methods + operators + iteration)
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v[0] = 9;
std::cout << v.size() << " [";
for (size_t i = 0; i < v.size(); ++i) std::cout << (i ? ", " : "") << v[i];
std::cout << "] " << v[0] << "\n";
return 0;
}
3 [9, 2, 3] 9
C# v11.0.100
// C# arrays are already objects (deriving from System.Array): Length/GetValue/iteration built in
int[] arr = { 1, 2 };
arr.SetValue(9, 0);
Console.WriteLine($"{arr.Length} [{string.Join(", ", arr)}] {arr.GetValue(0)}");
// List<T> is the most common growable wrapper
var list = new List<int> { 1, 2, 3 };
Console.WriteLine($"{list.Count} [{string.Join(", ", list)}]");
2 [9, 2] 9 3 [1, 2, 3]
Go v1.27.4
package main
import "fmt"
// Go in reverse: slices/maps are not objects (no methods) — hand-write a thin wrapper on demand
type List struct {
items []int
}
func (l *List) Add(v int) { l.items = append(l.items, v) }
func (l List) Count() int { return len(l.items) }
func (l List) String() string { return fmt.Sprint(l.items) }
func main() {
l := &List{}
l.Add(1)
l.Add(2)
l.Add(3)
fmt.Println(l.Count(), l.String())
}
3 [1 2 3]
Java v27.0.1
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Arrays.asList wraps an array in a fixed-size List view (writes go through to the array)
Integer[] backing = {1, 2};
List<Integer> view = Arrays.asList(backing);
view.set(0, 9);
System.out.println(view.size() + " " + view + " " + backing[0]);
// Wrapping in ArrayList gives a mutable collection (copy semantics)
List<Integer> copy = new ArrayList<>(Arrays.asList(1, 2, 3));
System.out.println(copy.size() + " " + copy);
}
}
2 [9, 2] 9 3 [1, 2, 3]
JavaScript v26.13.1
// JS in reverse: arrays and objects are already objects; key-value containers are object-like out of the box
const map = { a: 1, b: 2 };
map.c = 3; // Property assignment doubles as array-style writing
console.log(Object.keys(map).length);
console.log(JSON.stringify(map));
console.log(map.b);
console.log(typeof [], Array.isArray([]));
3
{"a":1,"b":2,"c":3}
2
object true
PHP v8.6.0
// ArrayObject: an objectified array — index read/write, count, iteration, getArrayCopy, the full OO interface set
$obj = new ArrayObject(["a" => 1, "b" => 2]);
$obj["c"] = 3; // ArrayAccess: array-style writing
echo $obj->count(), "\n";
echo json_encode($obj->getArrayCopy()), "\n";
echo $obj->offsetGet("b"), "\n";
3
{"a":1,"b":2,"c":3}
2
Python v3.15.2
# Python in reverse: lists/dicts are already objects (methods, subclassable), no wrapping needed
nums = [1, 2]
nums.append(3) # Method calls are the object-ness
nums[0] = 9 # The subscript protocol __setitem__
print(len(nums), nums)
print(isinstance(nums, object))
3 [9, 2, 3] True
Rust v1.99.1
// Rust in reverse: Vec ships with methods (len/push/iteration), no wrapper type needed
fn main() {
let mut v: Vec<i32> = Vec::new();
v.push(1);
v.push(2);
v.push(3);
v[0] = 9; // The subscript trait IndexMut
println!("{} {:?} {}", v.len(), v, v[0]);
}
3 [9, 2, 3] 9
TypeScript v26.13.1
// TS in reverse: containers are already objects; constrain key-value shapes with Record/interfaces at the type level
const map: Record<string, number> = { a: 1, b: 2 };
map.c = 3;
console.log(Object.keys(map).length);
console.log(JSON.stringify(map));
console.log(map.b);
3
{"a":1,"b":2,"c":3}
2
Language differences
C: arrays are bare memory with no methods — objectification means hand-writing a struct + functions (shown); glib's GArray/GPtrArray are mature wrappers in the C ecosystem.
C++: std::vector is already an object (RAII + methods + operators); the problem ArrayObject solves in PHP does not exist here.
C#: arrays inherit System.Array (implementing IList) — objects from birth; List<T>/Collection<T> are the everyday mutable wrappers.
Go: slices and maps are built-in reference-semantics types without methods — wrap in a small struct when you need domain semantics (shown); the standard library offers no generic wrapper class.
Java: arrays are objects but API-poor — Arrays.asList gives a fixed-size view, new ArrayList<>(...) copies into a mutable collection; know the difference between view and copy semantics.
JavaScript: arrays and objects are already objects (prototype methods) — the problem ArrayObject targets does not exist in JS.
PHP: arrays are value-type ordered hash maps (assignment copies) — ArrayObject wraps one into a reference-semantics object with the ArrayAccess/Countable/IteratorAggregate/Serializable quartet; the classic use is subclassing it into a collection with behavior (config objects, validating containers) — the only SPL structure whose core idea is wrapping.
Python: lists and dicts are already objects and subclassable (class MyList(list)) — the language design removes the need at the root.
Rust: Vec/HashMap ship full method and operator sets; extend via traits or newtype wrappers — similar in spirit to ArrayObject but stricter at compile time.
TypeScript: same as JavaScript — objects are the containers; TS adds type constraints (Record/interfaces) on top.