Jump to content

Recommended Posts

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 ......

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.)

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

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

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.