Page 1 of 1

Using returned array from function

Posted: Sat Feb 14, 2009 11:50 am
by cpetzol2
I have a function that returns an array(of various types) by value (well ... I hope its not a reference)

public function myClass::pullQueryMeta( ( int ) $identifier ) { stuff;stuff;stuff;   return $this->resource_list[$index_id]; }
 
// private myClass::resource_list[]; is indexed numerically, and is structured as such ...
resource_list [  
 [ 1,  array(5),  $sql_resource_reference,  'firstQuery',      array(10)    ],
 [ 1,  array(5),  $sql_resource_reference,  'secondQuery',  array(10)    ],
 [ 0,  array(5),  $sql_resource_reference,  'thirdQuery',     array(10)    ],  //This query failed...element [0]
 [ 1,  array(5),  $sql_resource_reference,  'fourthQuery',   array(10)    ],
                                     and so on ....
 [ 1,  array(5),  $sql_resource_reference,  'nthQuery',       array(10)    ]  
 ]

Code: Select all

 
//Snippet 1
if (  !($tmparr = $this->pullQueryMeta($resource_id)) || ($tmparr[0] != 1)  ) {
    failHandler();
} else ...
 
This is a little more bulky than I had hoped, not to mention the assignment of $tmparr, which is never used again outside of that conditional.

My original syntax was something like ...

Code: Select all

 
//Snippet 2
if (  $this->pullQueryMeta($resource_id)[0] != 1  ) {
    failHandler();
} else ...
 
... but I kept getting "Parser: Unexpected '[' ..."

The first snippet does work, but I really don't like the workaround. I feel like what I need is an explicit cast of the returned type, but I thought php handled types well on its own. Is there a syntax error that can be fixed, or is my usage of the returned type off base?

The array returned is not that large, and returning a complete copy is what I am after. In some cases I will keep a local copy around for further use, but in most cases I just want to check some major status flags and I am done with the array.

I would also prefer not to CLASS'ify the array structures and methods acting on them any more than I already have.

Thank you much.