dimkasmir Posted February 27, 2009 Share Posted February 27, 2009 This is the code for my class constructor as well as one function: class geocoin { function __construct($track) { $this->tracknum = $track; } function returnTrackNum() { return $this->tracknum; } However, the variable $tracknum does not set because when I run this code: $coins = new geocoin("test"); $num = $coins->returnTrackNum(); if (!isset($num)) { echo "fail"; } I can see that it is empty. Can someone tell me what I am doing wrong? Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/ Share on other sites More sharing options...
samshel Posted February 27, 2009 Share Posted February 27, 2009 define tracknum as member variable class geocoin { private tracknum; function __construct($track) { $this->tracknum = $track; } function returnTrackNum() { return $this->tracknum; } Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/#findComment-772235 Share on other sites More sharing options...
dimkasmir Posted February 27, 2009 Author Share Posted February 27, 2009 Now I get this errror: Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /homepages/13/d196500339/htdocs/geocoins/geocoin.php on line 4 Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/#findComment-772237 Share on other sites More sharing options...
samshel Posted February 27, 2009 Share Posted February 27, 2009 class geocoin { private $tracknum; function __construct($track) { $this->tracknum = $track; } function returnTrackNum() { return $this->tracknum; } oops ..forgot the $ sign... Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/#findComment-772240 Share on other sites More sharing options...
dimkasmir Posted February 27, 2009 Author Share Posted February 27, 2009 Nope, still getting the same error. Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/#findComment-772242 Share on other sites More sharing options...
samshel Posted February 27, 2009 Share Posted February 27, 2009 class geocoin { var $tracknum; function __construct($track) { $this->tracknum = $track; } function returnTrackNum() { return $this->tracknum; } try this.. Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/#findComment-772243 Share on other sites More sharing options...
trq Posted February 27, 2009 Share Posted February 27, 2009 That should be working fine, what version of php are you using? Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/#findComment-772244 Share on other sites More sharing options...
dimkasmir Posted February 27, 2009 Author Share Posted February 27, 2009 I'm not getting an error anymore but this time it's echoing fail again. Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/#findComment-772246 Share on other sites More sharing options...
samshel Posted February 27, 2009 Share Posted February 27, 2009 i think u r using PHP4... try this.. class geocoin { var $tracknum; function geocoin($track) { $this->tracknum = $track; } function returnTrackNum() { return $this->tracknum; } Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/#findComment-772248 Share on other sites More sharing options...
dimkasmir Posted February 27, 2009 Author Share Posted February 27, 2009 I've solved this problem: my host was defaulting to PHP 4. All I had to do was make a .htaccess file. Now I have a new problem. Later in the script I use the file_get_contents() function. However, the host has URL file-access disabled. Is there anyway to enable it without having access to the php.ini file? Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/#findComment-772251 Share on other sites More sharing options...
samshel Posted February 27, 2009 Share Posted February 27, 2009 u want to read a URL? Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/#findComment-772252 Share on other sites More sharing options...
dimkasmir Posted February 27, 2009 Author Share Posted February 27, 2009 yes Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/#findComment-772254 Share on other sites More sharing options...
samshel Posted February 27, 2009 Share Posted February 27, 2009 sorry ! i am not aware of a work around for this ! but that flag is specially defined to block such thing, so my assumption is it will be difficult to fo a workaround...but may be i am wrong. Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/#findComment-772256 Share on other sites More sharing options...
trq Posted February 27, 2009 Share Posted February 27, 2009 In your .htaccess.... php_flag allow_url_fopen on Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/#findComment-772257 Share on other sites More sharing options...
trq Posted February 27, 2009 Share Posted February 27, 2009 Actually, I think that will only work in versions lower than 4.3.4. It seems you need access to the ini file to enable that setting now. Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/#findComment-772258 Share on other sites More sharing options...
dimkasmir Posted February 27, 2009 Author Share Posted February 27, 2009 I was able to change this by making an ini file in the directory. Now after I launch the script a few times and everything displays correctly, its stops reading the page for some time. Then if I launch it again a little later it works but after a few times stops working again. Is there any way to fix this? Also, is it possible to read only a part of the file, not the whole thing? Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/#findComment-772264 Share on other sites More sharing options...
trq Posted February 27, 2009 Share Posted February 27, 2009 Is there any way to fix this? Wouldn't know without seeing the relevent code. Also, is it possible to read only a part of the file, not the whole thing? No. Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/#findComment-772265 Share on other sites More sharing options...
dimkasmir Posted February 27, 2009 Author Share Posted February 27, 2009 function getPage() { $this->page = file_get_contents("http://www.geocaching.com/track/details.aspx?tracker=".$this->tracknum); return $this->page; } function getName() { preg_match('#<span id="ctl00_ContentBody_lbHeading">(.+)</span>#', $this->getPage(), $match); if(isset($match[1]) && $match[1] != '') { $this->name = $match; return $this->name[1]; } else { return FALSE; } } Link to comment https://forums.phpfreaks.com/topic/147097-solved-variable-not-assigning-in-class-constructor/#findComment-772271 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.