Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 378901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:50:23+00:00 2026-05-12T14:50:23+00:00

Someone has suggested to e to use SplObjectStorage to keep track of a set

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:50:23+00:00Added an answer on May 12, 2026 at 2:50 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im having trouble trying to figure out some code someone has suggested and was
Someone has to be able to explain what I'm doing wrong here! I'm trying
Someone has an answer to this missing feature in Entity Framework. Does anyone have
Hope someone has an easy answer on this. I have a header image which
I'm hoping someone has seen this before because I can't for the life of
I'm hoping that someone has found a way of doing this already or that
I'm betting that someone has already solved this and maybe I'm using the wrong
I'm wondering if someone has already built a system for threaded comments (for lack
I am sure someone has this done already, and was hoping someone could share
I'm hoping someone has run into this sort of problem before, and can give

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.