1
iunderwood
Odd Object error under XOOPS 2.4.4

Recently, I upgraded my development environment to XOOPS 2.4.4, and I was noticing that somewhat randomly, I would get an error report about using getVar on a non-object.

The code in question was the return in this piece:

function uhqradio_username($uid) { 

    
$member_handler =& xoops_gethandler('member'); 
    
$user =& $member_handler->getUser($uid); 
     
    return 
$user->getVar('uname'); 
}


What I ended up doing was just adding a check to see if the piece was actually an object, and if not, just returning the number I passed over.

function uhqradio_username($uid) {

    
$member_handler =& xoops_gethandler('member');
    
$user =& $member_handler->getUser($uid);

    if (
is_object($user)) {
        return 
$user->getVar('uname');
    } else {
        return 
$uid;
    }
    
}


What I do notice is that every now and then, the list I process which calls this function will list the numberic ID for the first entry and then return the usernames for each subsequent entry. But this seems to only happen once every other day or so.

I'm not really all that familiar with object-orientation, so I'm not even sure where to begin looking at that sort of thing.

2
iHackCode
Re: Odd Object error under XOOPS 2.4.4

When the user is found it returns the 'uname' of that user. If the handler cannot find the user with that uid it assigns null to $user.
So it returns the $uid that was passed in (if user is not found)

if (is_object($user)) {
    return 
$user->getVar('uname'); //When the user is found it returns the 'uname' of that user.
} else {
    return 
$uid//Returns the $uid that was passed in (if user is not found)
}

3
iunderwood
Re: Odd Object error under XOOPS 2.4.4

Good to know.

I'm not sure why the UID goes unfound sometimes, but I don't really have much control of my hosting environment, so I'll pin the blame there. :)

Thanks for the explanation!

4
iHackCode
Re: Odd Object error under XOOPS 2.4.4

Possibly a deleted user?

5
trabis
Re: Odd Object error under XOOPS 2.4.4
  • 2010/9/18 14:23

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


Problem with

$member_handler =& xoops_gethandler('member');
$user =& $member_handler->getUser($uid);

is that get User always return a user! When user does not exist, it creates a new one. This is not the result one would expect, it should return false, not an empty object.

Anyway, do a check with $user->isNew() to see if you do have a new user or an existing one.

I had same problem with publisher, I was doing the same to update posts for users. In fact, instead of updating posts and if submiter was anonymous user(uid=0), Xoops would create a new User and update this new user posts, lol.

Ah, Got to love it.

6
iunderwood
Re: Odd Object error under XOOPS 2.4.4

Trabis,

I'm not so much trying to check if I have a user or not, but given a UID, I want it to return a name if I toss the numeric value at it. The error wasn't one I expected to run across since (typically) all the users should have accounts and such. Or at least they do in my test environment.

However, I can see the value where a user could be deleted, and instead of returning a numeric value, the function should return false instead.
++I;
Resized Image

Login

Who's Online

130 user(s) are online (119 user(s) are browsing Support Forums)


Members: 0


Guests: 130


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Dec 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits