Well I read the manuals on php.net, it says that ArrayObject can make objects work in array way, which enables easy iteration, counting and sorting. According to this definition the ArrayObject class should accept an object as its parameter. But the question is, does it work in the other way, in which an array can behave like an object with its elements as public properties? Lets say I have a user input $_POST array which contains username and email entered by user, will the following code below work flawlessly?

$input = array_map('parseinput', $_POST);
$input = new ArrayObject($input);
echo $input->username;
echo $input->email;
    $input = (object)array_map('parseinput', $_POST);
    
    echo $input->username
      Weedpacket;11004057 wrote:
      $input = (object)array_map('parseinput', $_POST);
      
      echo $input->username

      I see, this is a good way for an input array which is simply a stdclass that does not have or need to use any methods. I was wondering if ArrayObject will work out for other arrays that may require more functionality, since ArrayObject has lots of built-in methods such as ArrayObject::count(), ArrayObject::sort() and others.

        Sorry, are you talking about arrays or objects? Or is just this another theatre in your campaign to force PHP into being an object-oriented language? 🙂

          Weedpacket;11004114 wrote:

          Sorry, are you talking about arrays or objects? Or is just this another theatre in your campaign to force PHP into being an object-oriented language? 🙂

          I am talking about ArrayObject, an extension from PHP 5. It says that ArrayObject can make object properties work like array elements, but some examples on php.net illustrates that it can also convert arrays into objects. I am not sure if this is true though.

          And well, in a perfect program/script everything is an object. 🙂

            The ArrayObject(array) constructor doesn't convert an array to an object. Like the documentation (and syntax: [font=monospace]$array_object = new ArrayObject($array)[/font]) says, it creates a new ArrayObject object with elements from the array.

              Weedpacket;11004247 wrote:

              The ArrayObject(array) constructor doesn't convert an array to an object. Like the documentation (and syntax: [font=monospace]$array_object = new ArrayObject($array)[/font]) says, it creates a new ArrayObject object with elements from the array.

              Oh I see... Does this syntax work for the script you posted above?

              $array = array("element1", "element2");
              $array_object = new ArrayObject($array);
              echo $array_object->element1;
              echo $array_object->element2;
              

                No, because as the example on the manual page for ArrayObject::__construct shows, you would access those VALUES by their array KEYS. So instead of putting the VALUE after the object operator (which actually doesn't make much sense) you would put the key. so $array_object->0

                  Derokorian;11004255 wrote:

                  No, because as the example on the manual page for ArrayObject::__construct shows, you would access those VALUES by their array KEYS. So instead of putting the VALUE after the object operator (which actually doesn't make much sense) you would put the key. so $array_object->0

                  I see, thats too bad... This functionality looks completely the opposite as I wanted it to be. Guess I will have to give up my hope on ArrayObject extension then...

                    I'm confused how would the values of an array as the names of properties make sense? Makes more sense for the keys to be the property names and the values to be, well, the values. At least in my warped mind it does. It also makes more sense to do it with an associative array XD

                      bradgrafelman;11004258 wrote:

                      I'm equally confused.. with this:
                      ...What happens if you have something like:

                      $array = array("element1", "element1", "element1");

                      ? What value does this $array_object->element1 hold then?

                      Obviously, it holds either 0, 1, or 2. Or possibly TRUE. Or FALSE. Or maybe nothing.

                        Oh my goodness, thats a bad example. Forgive me for lack of sleep. A better one will be this associative array to object case:

                        $array = array("key1" => "var1", "key2" => "var2");
                        $array_object = new ArrayObject($array);
                        echo $array_object->key1;
                        echo $array_object->key2;
                        

                        But anyway it seems that ArrayObject extension does not do what I want, so I have decided to forget about it...

                          You can do this by overloading the get and set magic methods for a class. An object of that class will then hold an internal associative array (passed to the constructor) to which access is provided via the get and set methods.

                            umm looks like this flag will do the trick: ArrayObject::ARRAY_AS_PROPS

                            So the below code will work out nicely:

                            $array = array("key1" => "var1", "key2" => "var2");
                            $array_object = new ArrayObject($array, ArrayObject::ARRAY_AS_PROPS);
                            echo $array_object->key1;
                            echo $array_object->key2;
                            

                            Guess problem is resolved. Although it seems that ArrayObject by default does not produce an iterator object automatically, still have to write another line:

                            $array_iterator = $array_object->getiterator();
                            

                            to get this iterator. I understand why though, it is simply impossible to store this iterator as object property since all properties of an arrayobject must be elements in the corresponding array.

                              Write a Reply...