PHP Doku:: Returns the value at the specified index - arrayobject.offsetget.html

Verlauf / Chronik / History: (1) anzeigen

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

Ein Service von Reinhard Neidl - Webprogrammierung.

The ArrayObject class

<<ArrayObject::offsetExists

ArrayObject::offsetSet>>

ArrayObject::offsetGet

(PHP 5 >= 5.0.0)

ArrayObject::offsetGetReturns the value at the specified index

Beschreibung

mixed ArrayObject::offsetGet ( mixed $index )

Parameter-Liste

index

The index with the value.

Rückgabewerte

The value at the specified index or FALSE.

Beispiele

Beispiel #1 ArrayObject::offsetGet() example

<?php
$arrayobj 
= new ArrayObject(array('zero'7'example'=>'e.g.'));
var_dump($arrayobj->offsetGet(1));
var_dump($arrayobj->offsetGet('example'));
var_dump($arrayobj->offsetExists('notfound'));
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

int(7)
string(4) "e.g."
bool(false)


2 BenutzerBeiträge:
- Beiträge aktualisieren...
Alex Andrienko
20.01.2009 12:58
Speaking of offsetGet() method overloading, be advised, that if you're iterating through Object via foreach, this method wouldn't be called. Iterator's current() method will be called instead.
Sam
30.11.2007 12:01
If you're overloading ArrayObject, it's worth noting that while this method (when implemented by the parent) will return a reference, so code like $fakeArray['foobar']['hello'] = 1; will work like you expect.

However, when you overload the offsetGet method, you CANNOT define it as &offsetGet, so the above code falls out (because it returns the 'foobar' variable before you actually work with it).

This is something that the developers broke between 5.0 and 5.1, and was closed as bogus (http://bugs.php.net/bug.php?id=34783). So this is not a big, or question, or request, but just something worth noting.



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",...)