XpertWorlock Posted November 12, 2009 Share Posted November 12, 2009 Having a problem with a class, doesn't happen offline, but seems to occur when uploaded. PHP offline 5.3 PHP online 5.2.9 Class code class newsArticle{ function setDate($a){$this->date = unixToGregorian('j',$a);} function setTitle($a){$this->title = $a;} function setAuthor($a){$this->author = $a;} function setDescription($a){$this->description = $a;} function setID($a){$this->id = $a;} } calling of Class newsArticle $news[$i] = new newsArticle(); $news[$i]->setDate($row['date'] - (3600*5)); $news[$i]->setTitle(mysql_real_escape_string($row['title'])); $news[$i]->setAuthor($row['author']); $news[$i]->setDescription(mysql_real_escape_string($row['description'])); $news[$i]->setID($row['id']); $i++; The error I get is Catchable fatal error: Object of class newsArticle could not be converted to string in ...... Link to comment https://forums.phpfreaks.com/topic/181278-solved-class-could-not-be-converted-to-string/ Share on other sites More sharing options...
.josh Posted November 12, 2009 Share Posted November 12, 2009 what line # does the error say and point out which line that is Link to comment https://forums.phpfreaks.com/topic/181278-solved-class-could-not-be-converted-to-string/#findComment-956339 Share on other sites More sharing options...
XpertWorlock Posted November 12, 2009 Author Share Posted November 12, 2009 $news[$i] = new newsArticle(); Link to comment https://forums.phpfreaks.com/topic/181278-solved-class-could-not-be-converted-to-string/#findComment-956343 Share on other sites More sharing options...
PFMaBiSmAd Posted November 12, 2009 Share Posted November 12, 2009 That error generally only occurs when you try to use the instance of the class as though it was a string, such as - echo $news[$i]; Are you sure about the line where the error is occurring? Any chance that $i is an instance of a class? Your posted code does not produce any php error under php5.2.11 (I don't have php5.2.9 to test with.) Link to comment https://forums.phpfreaks.com/topic/181278-solved-class-could-not-be-converted-to-string/#findComment-956402 Share on other sites More sharing options...
Alex Posted November 12, 2009 Share Posted November 12, 2009 If you do want to use an instance of a class like a string it's useful to utilize the __toString() magic method. Ex: class someClass { public function __toString() { return 'Some name'; } } $in = new someClass; echo $in; // Some name Link to comment https://forums.phpfreaks.com/topic/181278-solved-class-could-not-be-converted-to-string/#findComment-956419 Share on other sites More sharing options...
XpertWorlock Posted November 12, 2009 Author Share Posted November 12, 2009 if(mysql_num_rows($result) > 0) { $i=1; while($row = mysql_fetch_array($result)) { $news[$i] = new newsArticle(); $news[$i]->setDate($row['date'] - (3600*5)); //SET TO 5, WILL HAVE TO CONFIGURE $news[$i]->setTitle(mysql_real_escape_string($row['title'])); $news[$i]->setAuthor($row['author']); $news[$i]->setDescription(mysql_real_escape_string($row['description'])); $news[$i]->setID($row['id']); $i++; } } The $i is just to go through an array. Like I said it works perfectly offline, doesn't want to work online though Link to comment https://forums.phpfreaks.com/topic/181278-solved-class-could-not-be-converted-to-string/#findComment-956436 Share on other sites More sharing options...
sasa Posted November 12, 2009 Share Posted November 12, 2009 try to declare $news as array before use it ($news=array() Link to comment https://forums.phpfreaks.com/topic/181278-solved-class-could-not-be-converted-to-string/#findComment-956442 Share on other sites More sharing options...
XpertWorlock Posted November 13, 2009 Author Share Posted November 13, 2009 Yeah that was my problem, I did not declare it before, although it worked offline, a safer programmer would have declared it before just to be safe. I'll make sure I know that for next time. Solution : Declare the array! Link to comment https://forums.phpfreaks.com/topic/181278-solved-class-could-not-be-converted-to-string/#findComment-956929 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.