Created
July 20, 2009 16:26
-
-
Save davidreuss/150428 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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