SplObjectStorage 不适用于 String,该怎么办?

发布于 2024-08-06 19:39:04 字数 140 浏览 5 评论 0原文

有人建议使用 SplObjectStorage 来跟踪一组独特的事物。很好,但它不适用于字符串。错误提示“ SplObjectStorage::attach() 期望参数 1 为对象,第 59 行 fback.php 中给出的字符串”

有什么想法吗?

Someone has suggested to e to use SplObjectStorage to keep track of a set of unique things. Great, except it doesn't work with strings. An error says " SplObjectStorage::attach() expects parameter 1 to be object, string given in fback.php on line 59"

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

自在安然 2024-08-13 19:39:04

SplObjectStorage 顾名思义:存储类用于存储对象。与其他一些编程语言相比,字符串在 PHP 中不是对象,它们是字符串;-)。因此,将字符串存储在 SplObjectStorage 中是没有意义的 - 即使您将字符串包装在 stdClass 类的对象中。

存储唯一字符串集合的最佳方法是使用数组(作为哈希表),以字符串作为键和值(如 伊恩·塞尔比)。

$myStrings = array();
$myStrings['string1'] = 'string1';
$myStrings['string2'] = 'string2';
// ...

不过,您可以将此功能包装到自定义类中:

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;
    }

    // ... 
}

顺便说一句,您可以模拟 PHP SplObjectStorage 的行为。 5.3.0 并更好地了解它的作用。

$ob1 = new stdClass();
$id1 = spl_object_hash($ob1);
$ob2 = new stdClass();
$id2 = spl_object_hash($ob2);
$objects = array(
    $id1 => $ob1,
    $id2 => $ob2
);

SplObjectStorage 为每个实例存储唯一的哈希值(例如 <代码>spl_object_hash()) 到
能够识别对象实例。正如我上面所说:字符串根本不是对象,因此它没有实例哈希。可以通过比较字符串值来检查字符串的唯一性 - 当两个字符串包含相同的字节集时,它们相等。

The SplObjectStorage is what its name says: a storage class for storing objects. In contrast to some other programming languages strings are not objects in PHP, they are, well, strings ;-). It therefore makes no sense to store strings in a SplObjectStorage - even if you wrap your strings in an object of class stdClass.

The best way to store a collection of unique strings si to use arrays (as hashtables) with the string as the key as well as the value (as suggested by Ian Selby).

$myStrings = array();
$myStrings['string1'] = 'string1';
$myStrings['string2'] = 'string2';
// ...

You could however wrap this functionality into a custom class:

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;
    }

    // ... 
}

By the way you san simulate the behavior of SplObjectStorage for PHP < 5.3.0 and to get a better understanding of what it does.

$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.

不再见 2024-08-13 19:39:04

它是一个对象存储。字符串是一个标量。因此,请使用 SplString

It's an Object Storage. A string is a scalar. So use SplString.

死开点丶别碍眼 2024-08-13 19:39:04

将字符串包装在 stdClass 中?

$dummy_object = new stdClass();
$dummy_object->string = $whatever_string_needs_to_be_tracked;
$splobjectstorage->attach($dummy_object);

但是,即使字符串相同,以这种方式创建的每个对象仍然是唯一的。

如果您需要担心重复的字符串,也许您应该使用散列(关联数组)来跟踪它们?

Wrap the string in a stdClass?

$dummy_object = new stdClass();
$dummy_object->string = $whatever_string_needs_to_be_tracked;
$splobjectstorage->attach($dummy_object);

However, every object created this way would still be unique, even if the strings are identical.

If you need to worry about duplicate strings, perhaps you should be using a hash (associative array) to track them instead?

淡淡離愁欲言轉身 2024-08-13 19:39:04
$myStrings = array();
$myStrings[] = 'string1';
$myStrings[] = 'string2';
...

foreach ($myStrings as $string)
{
    // do stuff with your string here...
}

如果你想确保数组中字符串的唯一性,你可以做一些事情......首先是简单地使用 array_unique()。那,或者您可以创建一个以字符串作为键和值的关联数组:

$myStrings = array();
$myStrings['string1'] = 'string1';
...

如果您想对此进行面向对象,您可以执行以下操作:

class StringStore
{
   public static $strings = array();

   // helper functions, etc.  You could also make the above protected static and write public functions that add things to the array or whatever
}

然后,在代码中您可以执行以下操作:

StringStore::$strings[] = 'string1';
...

并迭代同样的方式:

foreach (StringStore::$strings as $string)
{
    // whatever
}

SplObjectStorage 用于跟踪对象的唯一实例,除了不使用字符串之外,它对于您想要完成的任务来说有点矫枉过正(在我看来)。

希望有帮助!

$myStrings = array();
$myStrings[] = 'string1';
$myStrings[] = 'string2';
...

foreach ($myStrings as $string)
{
    // do stuff with your string here...
}

If you wanted to ensure uniqueness of strings in the array you could do a couple of things... first would be to simply use array_unique(). That, or you could create an associative array with the strings as keys as well as the values:

$myStrings = array();
$myStrings['string1'] = 'string1';
...

If you wanted to be object-oriented about this, you could do something like:

class StringStore
{
   public static $strings = array();

   // helper functions, etc.  You could also make the above protected static and write public functions that add things to the array or whatever
}

Then, in your code you can do stuff like:

StringStore::$strings[] = 'string1';
...

And iterate the same way:

foreach (StringStore::$strings as $string)
{
    // whatever
}

SplObjectStorage is for tracking unique instances of Objects, and outside of not working with strings, it's a bit overkill for what you're trying to accomplish (in my opinion).

Hope that helps!

初相遇 2024-08-13 19:39:04

或者也许只是使用 __toString() 方法将您的字符串实例化为对象 - 这样您就可以同时拥有它们 - 对象并能够将其用作字符串(var_dump,echo)。

Or Maybe just instantiate your string as an object with __toString() method - that way you can have them both - object and ability to use it as string (var_dump, echo)..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文