Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidreuss/150428 to your computer and use it in GitHub Desktop.
Save davidreuss/150428 to your computer and use it in GitHub Desktop.
<?php
class Collection extends SplObjectStorage {
public function __construct($mixed = array()) {
if ($mixed) {
$this->add($mixed);
}
}
public function add($mixed) {
if (is_object($mixed)) {
$this->attach($mixed);
}
if (is_array($mixed)) {
foreach ($mixed as $obj) {
$this->attach($obj);
}
}
}
}
class Tag_Collection extends Collection {
public function attach(Tag $tag) {
parent::attach($tag);
}
}
class Tag {
public $title;
public function __construct($title) {
$this->title = $title;
}
}
$tag1 = new Tag("One");
$tag2 = new Tag("Two");
$tag3 = new Tag("5ive");
$tag3Clone = clone $tag3;
$collection = new Tag_Collection(array($tag1, $tag2, $tag3));
$collection->add(new Tag("Six"));
$collection->add(new Tag("Six"));
$collection->add($tag3Clone);
foreach ($collection as $obj) {
echo $obj->title . "\n";
}
echo "===================\n";
$collection->detach($tag3Clone);
foreach ($collection as $obj) {
echo $obj->title . "\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment