SPL Data Structures

16 entries

ArrayIterator Function An iterator VIEW over an array: the Iterator protocol (rewind/valid/current/key/next) plus seek and array access, mutable while iterating; a different job from ArrayObject's container wrapping. ArrayObject Function Wraps an array into an object container (array access, counting, iteration, serialization), subclassable for extra behavior. iterator_count / iterator_apply Function Iterator helpers: iterator_count counts elements but consumes a forward iterator (the position does not survive); iterator_apply calls a callback per element (it must return true to continue) and returns the iteration count. LimitIterator / CallbackFilterIterator / RegexIterator Function The SPL decorator trio layered on iterators: limit (LimitIterator), callback-filter (CallbackFilterIterator), regex-filter (RegexIterator) - a lazy pipeline, complementing the concrete structures SplStack/SplQueue. iterator_to_array Function Materializes a lazy iterator/generator into an array; with preserve_keys=true later duplicates overwrite (manual-stated), false keeps all values with rebuilt keys. RecursiveIteratorIterator Function The outer wrapper that flattens a RecursiveIterator (directory tree, nested arrays) into one linear stream: LEAVES_ONLY by default, getDepth() reports level - recursive traversal is a scenario, not a function. spl_autoload_register Function Registers a function into the SPL autoload queue; it fires on demand the first time an undefined class is used. spl_object_id Function Unique integer handle for an object: same object always yields the same id, distinct objects differ (the numeric id is not pre-determined, so the example asserts booleans); identity-vs-equality compared across languages; replaces spl_object_hash (deprecated since 8.6). SplDoublyLinkedList Function The generic SPL doubly-linked list: O(1) push/unshift at both ends, pop/shift from both ends, FIFO/LIFO iteration modes; SplStack/SplQueue are thin subclasses of it. splfileobject Function The SPL object-oriented way to read files: foreach iterates lines directly (key is the line number, current the line), seek jumps to a line, setFlags(DROP_NEW_LINE) strips trailing newlines — a dual-track counterpart to procedural fopen/fgets, another stop on PHP's "procedural vs OOP" theme. splfixedarray Function A fixed-length array: its size is set at construction (changed manually via setSize), it accepts only integer keys, uninitialized slots are NULL, and it is memory-compact — a fundamental contrast to the regular PHP array (auto-growing, mixed keys, hash-bucket overhead); out-of-bounds access throws OutOfBoundsException. SplMinHeap / SplMaxHeap Function Ready-made subclasses of the abstract binary heap: SplMinHeap/SplMaxHeap order themselves via compare(), insert/extract O(log n); iterating a heap drains it. splobjectstorage Function A storage keyed by object identity: one slot per object; attach/contains/detach (deprecated since 8.5 — prefer the offset* API) can also carry associated data, serving as both an object set and an object-to-data map: the container form of identity-vs-equality (see spl_object_id). splpriorityqueue Function A max-heap priority queue: insert(value, priority) enqueues, extract dequeues, top peeks, isEmpty checks — extraction always yields the highest priority, but the order of equal-priority items is undefined (the demo uses distinct priorities for determinism); the extraction mode is tunable via setExtractFlags. SplQueue Function SPL's doubly-linked-list queue (FIFO) with O(1) enqueue and dequeue on both ends. SplStack Function SPL's doubly-linked-list stack (LIFO) with O(1) push and pop on both ends.