PHP Doku:: Construct a new array object - arrayobject.construct.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige GrunderweiterungenStandard PHP Library (SPL)Verschiedene Klassen und InterfacesThe ArrayObject classArrayObject::__construct

Ein Service von Reinhard Neidl - Webprogrammierung.

The ArrayObject class

<<ArrayObject::asort

ArrayObject::count>>

ArrayObject::__construct

(PHP 5 >= 5.0.0)

ArrayObject::__constructConstruct a new array object

Beschreibung

ArrayObject::__construct() ([ mixed $input [, int $flags [, string $iterator_class ]]] )

This constructs a new array object.

Parameter-Liste

input

The input parameter accepts an array or an Object.

flags

Flags to control the behaviour of the ArrayObject object.

iterator_class

Specify the class that will be used for iteration of the ArrayObject object. ArrayIterator is the default class used.

Rückgabewerte

Es wird kein Wert zurückgegeben.

Beispiele

Beispiel #1 ArrayObject::__construct() example

<?php
$array 
= array('1' => 'one',
               
'2' => 'two',
               
'3' => 'three');

$arrayobject = new ArrayObject($array);

var_dump($arrayobject);
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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

Siehe auch


5 BenutzerBeiträge:
- Beiträge aktualisieren...
ashley at nospam dot zincdigital dot com
4.07.2009 0:18
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.
kjarli at gmail dot com
3.07.2008 12:11
This extend allows multidimensional arrays to be converted aswell. It also returns 'Array' when echoed (unlike ArrayObject which gives an error).
<?php
/**
 * @author iltar van der berg
 * @version 1.0.1
 */
class RecursiveArrayObject extends ArrayObject
{
   
/**
     * overwrites the ArrayObject constructor for
     * iteration through the "array". When the item
     * is an array, it creates another self() instead
     * of an array
     *
     * @param Array $array data array
     */
   
public function __construct(Array $array)
    {   
        foreach(
$array as $key => $value) {
            if(
is_array($value)){
               
$value = new self($value);
            }
           
$this->offsetSet($key, $value);
        }
    }
   
   
/**
     * returns Array when printed (like "echo array();")
     * instead of an error
     *
     * @return string
     */
   
public function __ToString()
    {
        return
'Array';
    }
}
?>
german dot rumm at gmail dot com
14.03.2008 19:37
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
   
}
?>
agalkin at agalkin dot ru
21.08.2007 10:56
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
?>
Grigori Kochanov
15.07.2006 12:51
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



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)