Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,376
    Thanks
    263
    Thanked 32 Times in 31 Posts

    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


    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • Users who have thanked Fou-Lu for this post:

    durangod (10-02-2012)

  • #3
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,376
    Thanks
    263
    Thanked 32 Times in 31 Posts
    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

  • #4
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,376
    Thanks
    263
    Thanked 32 Times in 31 Posts
    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.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •