1

Here is my code of SessionManager.php:

private function generateAuthToken($user)
{
    $bits = '';

    $fp = @fopen('/dev/urandom', 'rb');
    if ($fp !== false) {
        $bits .= @fread($fp, 128);
        @fclose($fp);
    }

    return sha1($bits . time() . microtime() . $user->getUsername());
}

I'm getting this error:

Fatal error: Call to a member function getUsername() on a non-object in /home/shwetanka/src/Practo/PvrBundle/Manager/SessionManager.php on line 49

When I'm doing var_dump($user); right before the problem line I'm getting the full user object printed. I'm unable to understand the reason of this error. The function is present in the class User. Even if I try to call any other function of the class even then I get the same error for that function. I'm new to php and having hard time debugging this problem.

Also $user is object of User entity. I'm using symfony2 and this object is returned to me by using this:

$ur = $this->getDoctrine()->getRepository('PractoPvrBundle:User');
$user = $ur->findBy(array('email' => $email));
tereško
  • 58,060
  • 25
  • 98
  • 150
Shwetanka
  • 4,976
  • 11
  • 44
  • 68
  • If $user is not an object, I bet it's an array (based on the semantic of that findBy() method name. Try my answer below and let us know if that's not it.. – Chris Trahey Jul 13 '12 at 23:19

3 Answers3

2

Take the user out of the return like:

$username = $user->getUsername();
return sha1($bits . time() . microtime() . $username);

If this doesn't fix it, for debugging purpose you can try:

$username = 'N/A';
if(is_object($user) && method_exists($user, 'getUsername')) 
{
    $username = $user->getUsername();
}

Also you can cast your parameter $user like:

private function generateAuthToken(User $user) {...

This will throw an error if you get different class instance or non object

Alex Rashkov
  • 9,833
  • 3
  • 32
  • 58
  • I did try this. But still getting the same error. – Shwetanka Jul 13 '12 at 23:00
  • 1
    Wow... I'd have bet my house on that being the answer. PHP is full of "you must do this before you can do that" peculiarities where conversion/coercion is concerned. – Mitya Jul 13 '12 at 23:01
  • Yep when I check is_object($user) I'm getting false. But I get this user object as I've mentioned below in my question using symfony repository. How can this not be an object? – Shwetanka Jul 13 '12 at 23:10
  • 1
    Check that code: `$user = $ur->findBy(array('email' => $email)); ` and more specifically `$user`. If nothing is found in the DB for that email, e.g. there is no matching user you should get (bool)false instead of User object. And also double check the variable that is passed to generateAuthToken in the method that calls it, check for typo for example. – Alex Rashkov Jul 13 '12 at 23:20
2

Sometimes in the implicitly-convert-to-string context, PHP does not do well with method calls... I'm not sure why. Try this:

$username = $user->getUsername();
return sha1($bits . time() . microtime() . $username);

edit:

Also, I suspect that you are actually dealing with an array instead of an object. the line:

$user = $ur->findBy(array('email' => $email));

is probably intended to return many results. Use current() to get the first one:

$user = current($ur->findBy(array('email' => $email)));
Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
0
$ur = $this->getDoctrine()->getRepository('PractoPvrBundle:User');
$user = $ur->findBy(array('email' => $email));

-> findBy returns array of objects, use -> findOneBy