<?php
class Foo {
public $name;
public $type;
public function __construct($name, $type) {
$this->name = $name;
$this->type = $type;
}
public function searchZeroType($o) {
return ($o->type == 0);
}
public function doubleType($o) {
return $o->type = $o->type * 2; //Почему не получилось c return $o->type * 2;
}
}
class SplObjectFunctions {
public static function object_filter(callable $c, $objectStorage) {
$s = new SplObjectStorage();
foreach($objectStorage as $object) {
if ($c($object)) {
$s->attach($object);
}
}
return $s;
}
public static function object_map(callable $c, $objectStorage) {
$s = $objectStorage;
foreach($s as $object) {
$c($object);
}
return $s;
}
}
$a = new SplObjectStorage();
$a->attach(new Foo('first', 1));
$a->attach(new Foo('second', 1));
$a->attach(new Foo('third', 0));
$a->attach(new Foo('fourth', 1));
$search = SplObjectFunctions
::object_filter(array('Foo', 'searchZeroType'), $a); $double = SplObjectFunctions
::object_map(array('Foo', 'doubleType'), $a);
PD9waHAKY2xhc3MgRm9vIHsKCXB1YmxpYyAkbmFtZTsKCXB1YmxpYyAkdHlwZTsKCQoJcHVibGljIGZ1bmN0aW9uIF9fY29uc3RydWN0KCRuYW1lLCAkdHlwZSkgewoJCSR0aGlzLT5uYW1lID0gJG5hbWU7CgkJJHRoaXMtPnR5cGUgPSAkdHlwZTsKCX0KCQoJcHVibGljIGZ1bmN0aW9uIHNlYXJjaFplcm9UeXBlKCRvKSB7CgkJcmV0dXJuICgkby0+dHlwZSA9PSAwKTsKCX0KCQoJcHVibGljIGZ1bmN0aW9uIGRvdWJsZVR5cGUoJG8pIHsKCQlyZXR1cm4gJG8tPnR5cGUgPSAkby0+dHlwZSAqIDI7IC8v0J/QvtGH0LXQvNGDINC90LUg0L/QvtC70YPRh9C40LvQvtGB0YwgYyByZXR1cm4gJG8tPnR5cGUgKiAyOwoJfQp9CgpjbGFzcyBTcGxPYmplY3RGdW5jdGlvbnMgewoJcHVibGljIHN0YXRpYyBmdW5jdGlvbiBvYmplY3RfZmlsdGVyKGNhbGxhYmxlICRjLCAkb2JqZWN0U3RvcmFnZSkgewoJCSRzID0gbmV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