ArrayObject::__construct

(PHP 5, PHP 7, PHP 8)

ArrayObject::__constructConstruir un nuevo objeto Array

Descripción

public ArrayObject::__construct(mixed $input = array(), int $flags = 0, string $iterator_class = "ArrayIterator")

Esto crea un nuevo objeto array.

Parámetros

input

Este parámetro de input (entrada) acepta un array o un objeto.

flags

Flags para controlar el comportamiento del objeto ArrayObject.

iterator_class

Especifica la clase que va a ser usada para la iteración del objeto ArrayObject. ArrayIterator es la clase usada por omisión.

Valores devueltos

Devuelve un objeto ArrayObject en caso de éxito.

Errores/Excepciones

Lanza una InvalidArgumentException cuando:

  • input no es un array u objeto
  • flags no es un integer
  • iterator_class no es un objeto que implementa Iterator

Ejemplos

Ejemplo #1 Ejemplo de ArrayObject::__construct()

<?php
$array
= array('1' => 'uno',
'2' => 'dos',
'3' => 'tres');

$arrayobject = new ArrayObject($array);

var_dump($arrayobject);
?>

El resultado del ejemplo sería:

object(ArrayObject)#1 (3) {
  [1]=>
  string(3) "uno"
  [2]=>
  string(3) "dos"
  [3]=>
  string(5) "tres"
}

Ver también

add a note add a note

User Contributed Notes 4 notes

up
2
german dot rumm at gmail dot com
16 years ago
BTW, if you need to change array later, use exchangeArray() method. Good to know when you are writing a class that extends ArrayObject()

AFAIK, exchangeArray() doesn't return anything.

<?php
    $a
= array('one', 'two', 'three');
   
$ao = new ArrayObject($a);

    foreach (
$ao as $element) {
        echo
$element . ' '; // one two three
   
}

   
$b = array('four', 'five', 'six');
   
$ao->exchangeArray($b); // returns null

   
foreach ($ao as $element) {
        echo
$element . ' '; // four five six
   
}
?>
up
-2
ashley at nospam dot zincdigital dot com
15 years ago
The great confusion with this class is in its naming.  ArrayObject infers it will behave as an Array and as an Object.  It won't.  It behaves as an array.  It would better be called ArrayType.  You can, with some work, get it to work both as an object and as an array, but that is up to you.
up
-3
Grigori Kochanov
18 years ago
As Marcus explained, the flag ArrayObject::SPL_ARRAY_AS_PROPS means the array element may be used as a property if there is no conflict with visible properties.

If there are visible properties in the class, the array element will not overwrite it's value.

<?php
class Rules extends ArrayObject {
    public
$len = 1;
    function
__construct($array){
       
parent::__construct($array,ArrayObject::ARRAY_AS_PROPS);
       
$this['len'] = 2;
    }
}
$x = new Rules(array(1,2));
echo
$x->len;
?>
Result: 1

<?php
class Rules extends ArrayObject {
    private
$len = 1;
    function
__construct($array){
       
parent::__construct($array,ArrayObject::ARRAY_AS_PROPS);
       
$this['len'] = 2;
    }
}
$x = new Rules(array(1,2));
echo
$x->len;
?>
Result: 2
up
-7
agalkin at agalkin dot ru
17 years ago
Note that the first argument to ArrayObject::__construct, the initial array, is passed by reference. Nevertheless, modification of the array doesn't modify the object, so it may cause unexpected behaviour.

<?php
$array
= array('foo' => 'initial');
$obj = new ArrayObject($array);

// array was passed by reference:
$obj['foo'] = 'modified';
var_dump($array); // foo => modified

// but it doesn't work backwards:
$array['foo'] = 'modified_again';
var_dump($obj); // foo => modified
var_dump($array); // foo => modified_again
?>
To Top