连接对象和变量$ object-> $ variabile [PHP]

I try to make query to database, base on user input, because I have multiple inputs in single form it's a big more complicated.

I decide to check if input is filled like:

$query = "";
if((trim($searchParams->firstname)))
    $query .= "firstname,";

which works correctly for me, and after I check each input I explode string to array by , and foreach it

$querys = explode(",", $query);
foreach($querys as $q)
{
 ...
}   

What basically create an array of all filled inputs, however just their names base on what i need to get values as well. So I try to add something like this to query

$searchParams->$q;

But this doesn't work (say it's empty despite the fact if i echo it before it's filled)

I try to do something really dirt as:

    $param = "$searchParams->".$q;
    $values .= $param;

which say

Object of class could not be converted to string ( if i var_dump($q) it say its string so i dont understand)

I think I 'm doing it really badly but thats the only thing I found out as potentially useful. Any advise to fix this will be helpful.

p.s. Here is how final query should looks like:

"SELECT * FROM candidates WHERE firstname = ? AND surname = ?" ,$searchParams->firstname, $searchParams->surname