webref.eu Posted January 18, 2010 Share Posted January 18, 2010 I have a class which defines the $sQuery variable as per the below: class shopcore { var $iMerchantId; var $iCategoryId = 0 ; var $iProductId; var $sQuery; later in the class, sQuery gets set as follows: $this->sQuery = strlen($_GET['q']) > 0 ? trim( strip_tags( urldecode($_GET['q']) ) ) : null; This whole class gets included in a file, let's call it sony-laptop.php. Thing is, I want to manually set $sQuery to have a value of "sony laptop". I don't want to use the querystring q to set this, I just want to manually define another variable in my sony-laptop.php file, lets called it $KeyphrasePage, and then set $sQuery to this. I've tried stuff like this: Early in sony-laptop.php file, define the first variable: $KeyphrasePage = "sony laptop"; Then within class try and set sQuery to this: $this->sQuery = $KeyphrasePage; but it's not getting set at all, it seems $KeyphrasePage isn't passed into the class. :'( So how to I pass my $KeyphrasePage into the class properly and then set sQuery to it??? Any help much appreciated. Thanks Link to comment https://forums.phpfreaks.com/topic/188937-experts-please-help-setting-a-variable-within-a-class/ Share on other sites More sharing options...
phonydream Posted January 18, 2010 Share Posted January 18, 2010 In your shopCore class file, put the following: function set_sQuery($yourVariable){ $this->sQuery= $yourVariable; } Then, in sony-laptop.php you can set the value by: $KeyphrasePage = 'sony laptop'; $shopCore->set_sQuery($KeyphrasePage); Link to comment https://forums.phpfreaks.com/topic/188937-experts-please-help-setting-a-variable-within-a-class/#findComment-997608 Share on other sites More sharing options...
phonydream Posted January 18, 2010 Share Posted January 18, 2010 Also, you might add the following to your shopCore class file: function get_sQuery(){ return $this->sQuery; } Then in sony-laptop.php you can use: $shopCore->get_sQuery; to get the current value of $sQuery; Link to comment https://forums.phpfreaks.com/topic/188937-experts-please-help-setting-a-variable-within-a-class/#findComment-997610 Share on other sites More sharing options...
webref.eu Posted January 18, 2010 Author Share Posted January 18, 2010 Thanks for your reply. I still haven't quite got it working. The class, which is being included in the master file, sony-laptop.php, is: class shopcore { var $sQuery; Then within this is the function: public function gatherEnvData() Which contains: if ( strlen($_GET['q']) > 0 ) { $this->sQuery = strlen($_GET['q']) > 0 ? trim( strip_tags( urldecode($_GET['q']) ) ) : null; } else { $this->sQuery = $KeyphrasePage; I'm still not managing to set sQuery, have I got the lines in the original file wrong? In original file I have: $KeyphrasePage = "sony"; $gatherEnvData->set_sQuery($KeyphrasePage); Thanks Link to comment https://forums.phpfreaks.com/topic/188937-experts-please-help-setting-a-variable-within-a-class/#findComment-997626 Share on other sites More sharing options...
phonydream Posted January 18, 2010 Share Posted January 18, 2010 Your code doesn't make sense to me. I think urldencode should be urlencode though. Edit: my mistake, you're right. urldencode is correct (the opposite of urlencode!!). Do you have the set_sQuery method defined? Also, are you sure sQuery isn't an empty value (rather than not being set)? Link to comment https://forums.phpfreaks.com/topic/188937-experts-please-help-setting-a-variable-within-a-class/#findComment-997640 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.