The SplObjectStorage class

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

Einführung

The SplObjectStorage class provides a map from objects to data or, by ignoring data, an object set. This dual purpose can be useful in many cases involving the need to uniquely identify objects.

Klassenbeschreibung

class SplObjectStorage implements Countable, Iterator, Serializable, ArrayAccess {
/* Methoden */
public addAll(SplObjectStorage $storage): int
public attach(object $object, mixed $info = null): void
public contains(object $object): bool
public count(int $mode = COUNT_NORMAL): int
public current(): object
public detach(object $object): void
public getHash(object $object): string
public getInfo(): mixed
public key(): int
public next(): void
public offsetExists(object $object): bool
public offsetGet(object $object): mixed
public offsetSet(object $object, mixed $info = null): void
public offsetUnset(object $object): void
public removeAll(SplObjectStorage $storage): int
public removeAllExcept(SplObjectStorage $storage): int
public rewind(): void
public serialize(): string
public setInfo(mixed $info): void
public unserialize(string $data): void
public valid(): bool
}

Beispiele

Beispiel #1 SplObjectStorage as a set

<?php
// As an object set
$s = new SplObjectStorage();

$o1 = new StdClass;
$o2 = new StdClass;
$o3 = new StdClass;

$s->attach($o1);
$s->attach($o2);

var_dump($s->contains($o1));
var_dump($s->contains($o2));
var_dump($s->contains($o3));

$s->detach($o2);

var_dump($s->contains($o1));
var_dump($s->contains($o2));
var_dump($s->contains($o3));
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
bool(false)

Beispiel #2 SplObjectStorage as a map

<?php
// As a map from objects to data
$s = new SplObjectStorage();

$o1 = new StdClass;
$o2 = new StdClass;
$o3 = new StdClass;

$s[$o1] = "data for object 1";
$s[$o2] = array(1,2,3);

if (isset(
$s[$o2])) {
    
var_dump($s[$o2]);
}
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

Inhaltsverzeichnis

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Was genau bedeutet "Vibe Coding"? Ein tiefgehender Blick für Entwickler

In der Welt der Softwareentwicklung gibt es unzählige Wege, wie man an ein Projekt herangeht. Manche schwören auf strikte Planung, andere auf bewährte Algorithmen und wieder andere lassen sich von etwas ganz anderem leiten: ihrem Gefühl. ...

admin

Autor : admin
Kategorie: Software & Web-Development

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

Bild drehen

Looking for a fast and easy way to isolate vocals from any song? Try Voice Isolator (https://www.voiceisolator.org/) – a free online tool that l ...

Geschrieben von ww121314156 am 08.06.2025 15:18:13
Forum: PHP Developer Forum
Bild drehen

Your dedication is commendable—ten hours of persistence shows true determination. With the right guidance, success is always within reach! Just ...

Geschrieben von mobhsol am 06.06.2025 14:52:51
Forum: PHP Developer Forum
Bild drehen

Your persistence is truly inspiring—spending over 10 hours on a task shows unmatched dedication and a genuine thirst for learning. Just like rot ...

Geschrieben von mobhsol am 06.06.2025 14:49:12
Forum: PHP Developer Forum
Bild drehen

Your skill in adjusting visuals enhances user experience beautifully. I’m a trusted pediatric dietitian and nutritionist solving kids’ health. ...

Geschrieben von hsolsuraj009 am 06.06.2025 13:56:54
Forum: PHP Developer Forum