Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-02-2012, 03:41 AM   PM User | #1
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
Non object error

Hi i noticed this error the other day on my cpanel log and i have not been able to figure out a pattern as to whats happening to cause this.

Here is the error

Quote:

PHP Fatal error: Call to a member function next() on a non-object in /home/xxxxxxx/public_html/xxxxxxxxxxx/flashchat/chat/inc/classes/chatServer.php on line 205
And here is the function line 205 is of course the line containingn next()


PHP Code:

function getUsers() 
  { 
    
ChatServer::prepare('getUsers'); 


    
$ret = array(); 
    
$res $GLOBALS['fc_config']['cms']->getUsers(); 
    if(
is_array($res)) 
    { 
      foreach(
$res as $k=>$v
      { 
        
$v['login'] = htmlentities(stripslashes($v['login'])); //Sanitizing input - Rodolfo Andrade @ 2008-11-17 - 14:51
         
$ret[$v['id']] = $v
      } 
    } 
    else 
      while(
$rec $res->next()) $ret[$rec['id']] = $rec

    return 
$ret
thanks.
durangod is offline   Reply With Quote
Old 10-02-2012, 05:37 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This line doesn't return an object: $res = $GLOBALS['fc_config']['cms']->getUsers();.
It can be easily "fixed" so it doesn't issue a fatal error with:
PHP Code:
if ($res instanceof Traversable)
{
    while (
$rec $res->next())
    {
        
$ret[$rec['id']] = $rec;
    }

This data looks like $rec is supposed to be a type of ArrayObject. At minimum it has to implement both Iterator (or a subclass) as well as ArrayAccess to be used as you have it here.
While this will fix the error you'll end up with nothing in $ret. $res must be either an array as specified here, or an object to use that while, so use else if ($res instanceof Traversable) instead. Null can be checked against an instanceof operation as well without triggering an error.

Edit:
Actually, this cannot be a Traversable type. next() doesn't return an object according to the iterator API, so that would be done with:
PHP Code:
while ($obj->valid())
{
    
$cur $obj->current();
    
$obj->next();

Or just using a foreach.

Last edited by Fou-Lu; 10-02-2012 at 05:39 AM..
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
durangod (10-02-2012)
Old 10-02-2012, 05:50 AM   PM User | #3
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
Thanks Fou-Lu appreciate that, ill work on that tomorrow to get that fixed.

On a different note is it just me or is africa hard at it tonight, of my 20 sites, most of them are getting hit hard with scripts from ip 41. Earlier tonight we had a global dns server go down and now my cpanel error log are turning over with denies lol..

Just curious if anyone else had this issue tonight. Thanks again. ;0
durangod is offline   Reply With Quote
Old 10-03-2012, 08:08 AM   PM User | #4
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
Fou-Lu Just wanted to get back to you, i know you said the next could not be used to return an object, i read the same thing on the doc site.

But i tried the other and it did not work, so i tried that just to see, and no more error. Really strange.

This is how it looks.. Just thought you might like to know the resolve on this..

PHP Code:

function getUsers()
  {
    
ChatServer::prepare('getUsers');


    
$ret = array();
    
$res $GLOBALS['fc_config']['cms']->getUsers();
    if(
is_array($res))
    {
      foreach(
$res as $k=>$v)
      {
        
$v['login'] = htmlentities(stripslashes($v['login'])); //sanitize
        
$ret[$v['id']] = $v;
      }
    }elseif(
$res instanceof Traversable)
         {
           while (
$rec $res->next())
           {
            
$ret[$rec['id']] = $rec;
           }
//close while
        
}//close elseif  

 
return $ret;
 }
//close function 

Last edited by durangod; 10-03-2012 at 08:11 AM..
durangod is offline   Reply With Quote
Old 10-03-2012, 02:53 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Going through the ArrayObject here as well in case that's what this is, it doesn't implement Iterator directly, only Traversable, so if it were an arrayobject you cannot use next, valid, or current on it. Given the original implementation would indicate this is a custom object, not a Traversable one.
So the first thing you need to do is figure out what this is supposed to be. Since you are manipulating the globals (which you shouldn't be doing), you'll need to trace the code back to the creation of this variable, then evaluate the function getUsers within it to see what is possible to return. Then you can use if/else blocks to determine what to do with it.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:45 PM.


Advertisement
Log in to turn off these ads.