Passing variable to object

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

User avatar
sscarano
New php-forum User
New php-forum User
Posts: 4
Joined: Sun May 19, 2013 9:17 am
Location: Buenos Aires, Argentina

Passing variable to object

Postby sscarano » Sun May 19, 2013 9:27 am

Hi, Im new both to php and this forum. Hope you can help me with a problem.
I have the following code, I want to pass $user variable to an object in the following line: "$result = $o_search->search('modified.$user:"2013"'); "
I tried setting $user to global but it doesn't work. I also tried "$o_search = new ObjectSearch($user);"
But no luck either. I think I need to use a constructor, but I can't figure it out how.
The code bellow works, but it doesn't take the value from $user
Any help would be very much appreciated.
Excuse my English, it's not my native language.

//CODE
<?php
$po_request = $this->getVar('request');
$vs_widget_id = $this->getVar('widget_id');
$user = $this->getVar('user');

$o_search = new ObjectSearch();
$result = $o_search->search('modified.$user:"2013"'); //Here the value from $user does not get passed, if i replace $user with $user actual content the code works like it should.

Print "You selected user: $user"."<br/>\n"; //Here the value does get printed
$count = 1;
while($result->nextHit()) {
print "Hit ".$count.": ".$result->get('ca_objects.preferred_labels.name')."<br/>\n";
$count++;
}


?>

User avatar
sscarano
New php-forum User
New php-forum User
Posts: 4
Joined: Sun May 19, 2013 9:17 am
Location: Buenos Aires, Argentina

Re: Passing variable to object

Postby sscarano » Wed May 22, 2013 3:53 am

In case it is useful for anyone the way to pass properties to objects, in this case implicitly (not recomended for debugging) is:

$o_search -> name = blah;
$o_search-> type = 1234;
etc.

Still can´t use those values inside the method 'search', couple more test and I hope I'll get it.
I think I´ll write a new class that inherits the one that I need and add the properties I need.
Bye.

User avatar
sscarano
New php-forum User
New php-forum User
Posts: 4
Joined: Sun May 19, 2013 9:17 am
Location: Buenos Aires, Argentina

Re: Passing variable to object

Postby sscarano » Thu May 23, 2013 4:26 pm

What i needed was
$result = $o_search->search( $o_search->chtype. ".". $o_search->user. '.:"' . $o_search->date . '"' );
I had to respect the punctuation that search method requires.
Thanks me!
Yo are wellcome dude! :D
P.D: Objects have properties, not variables xD


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron