SplObjectStorage
is what its name says: a storage class for storing objects. In contrast to some other programming languages strings
onlar, iyi, dizeleri ;-) vardır, PHP nesneler değildir. Eğer sınıfın bir nesne içinde dizeleri sarın bile stdClass
- Bu nedenle bir SplObjectStorage
dizeleri depolamak için hiçbir mantıklı.
Eşsiz dizeleri bir koleksiyonu saklamak için en iyi yolu (Ian Selby tarafından önerilen) tuşu yanı sıra değer olarak dize ile (hashtables gibi) diziler kullanmak si.
$myStrings = array();
$myStrings['string1'] = 'string1';
$myStrings['string2'] = 'string2';
// ...
Ancak özel bir sınıf bu işlevselliği sarın:
class UniqueStringStorage // perhaps implement Iterator
{
protected $_strings = array();
public function add($string)
{
if (!array_key_exists($string, $this->_strings)) {
$this->_strings[$string] = $string;
} else {
//.. handle error condition "adding same string twice", e.g. throw exception
}
return $this;
}
public function toArray()
{
return $this->_strings;
}
// ...
}
Eğer san PHP SplObjectStorage davranışını simüle arada 5.3.0 ve ne yaptığını daha iyi anlamak için.
$ob1 = new stdClass();
$id1 = spl_object_hash($ob1);
$ob2 = new stdClass();
$id2 = spl_object_hash($ob2);
$objects = array(
$id1 => $ob1,
$id2 => $ob2
);
SplObjectStorage
stores a unique hash for each instance (like spl_object_hash()
) to
be able to identify object instances. As I said above: a string is not an object at all, it therefore does not have an instance hash. A string's uniqueness can be checked by comparing the string values - two strings are equal when they contain the same set of bytes.