php8.5
Home/ Manual/ spl / arrayobject/ ArrayObject::__construct

ArrayObject::__construct

PHP function Edit on GitHub ✎

(PHP 5, PHP 7, PHP 8)

Construct a new array object

Description

ArrayObject::__construct(array|object $array = [], int $flags = 0, string $iteratorClass = ArrayIterator::class)

This constructs a new array object.

Warning

As of PHP 8.5.0, passing an enum as the array parameter throws an InvalidArgumentException. Modifying the $name or $value properties of an enum would break engine assumptions.

Parameters

array

The array parameter accepts an array or an object. Passing an object is deprecated as of PHP 8.5.0.

flags

Flags to control the behaviour of the ArrayObject object. See ArrayObject::setFlags.

iteratorClass

Specify the class that will be used for iteration of the ArrayObject object. The class must be a subtype of the ArrayIterator class.

Errors/Exceptions

As of PHP 8.5.0, throws an InvalidArgumentException if array is an enum.

Changelog

VersionDescription
8.5.0Passing an enum as array now throws an InvalidArgumentException.
8.5.0Passing an object as array is deprecated.

Examples

ArrayObject::__construct() example

php
<?php

$array = [
    '1' => 'one',
    '2' => 'two',
    '3' => 'three'
];

$arrayobject = new ArrayObject($array);

var_dump($arrayobject);

?>

The above example will output:

output
object(ArrayObject)#1 (1) {
  ["storage":"ArrayObject":private]=>
  array(3) {
    [1]=>
    string(3) "one"
    [2]=>
    string(3) "two"
    [3]=>
    string(5) "three"
  }
}

See Also

  • ArrayObject::setFlags
  • ArrayObject::getArrayCopy
  • ArrayIterator::__construct

Source: reference/spl/arrayobject/construct.xml · from the official PHP manual (php/doc-en)