PHP Basics

StrictSplDatastructureTrait

The common interface of all type-sensitive, SPL-based datastructures.

This extends all methods of the common interface of the Standard PHP Library Doubly Linked List Datastructures by type-checking to only allow specified data types on the list.

Tags
author

Sebastian Meyer sebastian.meyer@opencultureconsulting.com

template

AllowedType of mixed

Table of Contents

Methods

__construct()  : void
Create a type-sensitive, traversable list of items.
add()  : void
Add/insert a new item at the specified offset.
append()  : void
Append items at the end of the list.
clear()  : void
Clear the list of any items.
get()  : AllowedType
Get the item at the specified index.
isList()  : true
Check if this can be considered a list.
offsetSet()  : void
Set the item at the specified offset.
prepend()  : void
Prepend items at the start of the list.
push()  : void
Push an item at the end of the list.
remove()  : void
Remove an item from the list.
set()  : void
Set an item at the specified index.
toArray()  : array<int, AllowedType>
Return array representation of list.
toStrictCollection()  : StrictCollection<string|int, AllowedType>
Turn list into a type-sensitive collection.
unshift()  : void
Prepend the list with an item.

Methods

__construct()

Create a type-sensitive, traversable list of items.

public __construct([array<string|int, string> $allowedTypes = [] ]) : void
Parameters
$allowedTypes : array<string|int, string> = []

Allowed data types of items (optional)

If empty, all types are allowed. Possible values are:

  • "array"
  • "bool"
  • "callable"
  • "countable"
  • "float" or "double"
  • "int" or "integer" or "long"
  • "iterable"
  • "null"
  • "numeric"
  • "object" or FQCN
  • "resource"
  • "scalar"
  • "string"
Tags
throws
InvalidArgumentException

if any value of $allowedTypes is not a string

add()

Add/insert a new item at the specified offset.

public add(int $offset, AllowedType $value) : void
APIYes
Parameters
$offset : int

The offset where the new item is to be inserted

$value : AllowedType

The new item for the offset

Tags
throws
InvalidDataTypeException

if $value is not of allowed type

throws
OutOfRangeException

when $offset is out of bounds

get()

Get the item at the specified index.

public get(int $offset) : AllowedType
APIYes
Parameters
$offset : int

The item's index

Tags
throws
OutOfRangeException

when $offset is out of bounds

Return values
AllowedType

The item

isList()

Check if this can be considered a list.

public isList() : true
APIYes
Return values
true

Always TRUE (this exists only for compatibility reasons)

offsetSet()

Set the item at the specified offset.

public offsetSet(int|null $offset, AllowedType $value) : void
APIYes
Parameters
$offset : int|null

The offset being set or NULL to append

$value : AllowedType

The new item for the offset

Tags
throws
InvalidDataTypeException

if $value is not of allowed type

throws
OutOfRangeException

when $offset is out of bounds

prepend()

Prepend items at the start of the list.

public prepend(AllowedType ...$values) : void
APIYes
Parameters
$values : AllowedType

One or more items to prepend

Tags
throws
InvalidDataTypeException

if $value is not of allowed type

remove()

Remove an item from the list.

public remove(int $offset) : void
APIYes
Parameters
$offset : int

The item's index

Tags
throws
OutOfRangeException

when $offset is out of bounds

set()

Set an item at the specified index.

public set(int $offset, AllowedType $value) : void
APIYes
Parameters
$offset : int

The item's index

$value : AllowedType

The item

Tags
throws
InvalidDataTypeException

if $value is not of allowed type

toArray()

Return array representation of list.

public toArray() : array<int, AllowedType>
APIYes
Return values
array<int, AllowedType>

Array of list items


        
On this page

Search results