title: PHP function return value nested array removed
body: <p>I have a function that builds a collection of user objects from the database:</p>
  
  <pre><code>public static function GetUsersByGroup($instanceID, $groupID)
  {               
      $col = null;
      if($groupID != null) 
      {
          $col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_USERGROUP_MEMBERS,array ($instanceID, $groupID));
      }
      else
      {
          $col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_NOGROUP_MEMBERS,$instanceID);
      }
      echo "this is the collection I am going to return: &lt;pre&gt;";
      print_r($col);
      echo "&lt;/pre&gt;";
      return $col;
  }
  </code></pre>
  
  <p>The method has some debug output at the bottom, but the point is if I call that method with a null groupid param i.e it runs the second condition, it prints out a nice indication of the collection that I expected to receive, which is great.</p>
  
  <p>However ..</p>
  
  <p>Here is my calling method:</p>
  
  <pre><code>             echo "&lt;br&gt; Collection passed through is: &lt;/br&gt;";
               $collection =  UserGroup::GetUsersByGroup($this-&gt;GetInstance()-&gt;id,$grouplist-&gt;GetCurrentCommandParam());
               print_r($collection);
               $userlist-&gt;UpdateCollection($collection);
               $userlist-&gt;DeSelect();
  </code></pre>
  
  <p>The intresting thing is the output:</p>
  
  <pre><code>  this is the collection I am going to return: 
  Collection Object
  (
      [_valueType:protected] =&gt; User
      [_isBasicType:protected] =&gt; 
      [_validateFunc:protected] =&gt; 
      [_collection:protected] =&gt; Array
          (
              [0] =&gt; User Object
                  (
                      [valid] =&gt; 
                      [validationMessage] =&gt; 
                      [id] =&gt; 29
                      [table:private] =&gt; user
                      [fields:private] =&gt; Array
                          (
                              [title] =&gt; mrs
                              [fname] =&gt; Kirsty
                              [lname] =&gt; Howden
                              [email] =&gt; kirsty2@softyolk.com
                              [password] =&gt; xxxxxxxx
                              [lastlogin] =&gt; 2009-07-05 15:20:13
                              [instanceID] =&gt; 2
                              [deliveryAddress] =&gt; 
                              [invoiceAddress] =&gt; 
                              [tel] =&gt; 01752848484
                              [isAdmin] =&gt; 0
                              [disabled] =&gt; 0
                              [mustAuthorise] =&gt; 
                              [usergroupID] =&gt; 
                          )
  
                      [validationRules:private] =&gt; Array
                          (
                          )
  
                      [_profileStartTime:protected] =&gt; 
                      [_profileTag:protected] =&gt; 
                  )
  
              [1] =&gt; User Object
                  (
                      [valid] =&gt; 
                      [validationMessage] =&gt; 
                      [id] =&gt; 31
                      [table:private] =&gt; user
                      [fields:private] =&gt; Array
                          (
                              [title] =&gt; master
                              [fname] =&gt; Seb
                              [lname] =&gt; Howden
                              [email] =&gt; seb@antithug.co.uk
                              [password] =&gt; xxxxxxxxx
                              [lastlogin] =&gt; 2009-07-09 02:02:24
                              [instanceID] =&gt; 2
                              [deliveryAddress] =&gt; saltash
                              [invoiceAddress] =&gt; saltash
                              [tel] =&gt; 8908908
                              [isAdmin] =&gt; 0
                              [disabled] =&gt; 0
                              [mustAuthorise] =&gt; 
                              [usergroupID] =&gt; 
                          )
  
                      [validationRules:private] =&gt; Array
                          (
                          )
  
                      [_profileStartTime:protected] =&gt; 
                      [_profileTag:protected] =&gt; 
                  )
  
          )
  
  )
  
  Collection passed through is: 
  this is the collection I am going to return: 
  Collection Object
  (
      [_valueType:protected] =&gt; User
      [_isBasicType:protected] =&gt; 
      [_validateFunc:protected] =&gt; 
      [_collection:protected] =&gt; Array
          (
          )
  
  )
  Collection Object ( [_valueType:protected] =&gt; User [_isBasicType:protected] =&gt; [_validateFunc:protected] =&gt; [_collection:protected] =&gt; Array ( ) )
  </code></pre>
  
  <p>The object returned has been modified??</p>
  
  <p>If the GetUsersByGroup method is called with a userGroupID  i.e the first case, then output is all as expected.</p>
  
  <p>If i remove the conditional from the method and simply return <code>$col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_NOGROUP_MEMBERS,$instanceID);</code> then all output is as expected.</p>
  
  <p>It seems that the else condition executes correctly, and then is corrupted on return, but this only happens if the else condition is present, remove the else condition, and simply return the result of the method call in the else condition, and all is as expected.</p>
  
  <p>Any idea please?</p>
  
  <p>Thanks</p>
  
  <p>ADDED THE UserGroup::GetCollection Method (this is a deep rabbit hole though, could go on) </p>
  
  <pre><code>protected static function GetCollection($class, $sqlID, $params = null)
  {
      $dal = DAL::GetInstance(); //not to be confused with the Instance object, this is an instance of DAL        
  
      $collection = new Collection($class);
      $items = $dal-&gt;QueryForAssoc($sqlID,$params);
  
      foreach($items as $item)
      {
            $itemObject = new $class();
            $itemObject-&gt;LoadFromList($item);
            $collection-&gt;add($itemObject);
      }
  
      return $collection;        
  }
  </code></pre>
  
  <p>To further clarify the follwing works fine ::</p>
  
  <pre><code>public static function GetUsersByGroup($instanceID, $groupID)
  {               
      $col = null;
      //if($groupID != null) 
      //{
          //$col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_USREGROUP_MEMBERS,array ($instanceID, $groupID));
      //}
      //else
      //{
          $col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_NOGROUP_MEMBERS,$instanceID);
     // } 
     return $col; 
  }
  </code></pre>
  
  <p>I only see the issue if the line is in the else block.</p>