1,105,644 Community Members

Call to a member function on a non-object - strange problem

Member Avatar
newprimitive
Newbie Poster
21 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

I have problem with a simple login script, keep getting message "Call to a member function on a non-object". The same code worked locally, but when i tried to run it online i got this message.

have two php files:login.php and connect.php

here is simplified code from connect.php

class Connect{
   var $logged;
   function check_login($uname, $pass){
      $this->logged=true;                  // works - i checked with die() function
      $this->set_session(logged', true);   //works
      ..
   }
   function simple(){
      return $_SESSION['data'];
   }
}
$user=new Connect;

here is the code from user.php

include_once('connect.php');
$user->check_login($uname, $pass);          // works
var_dump($user->logged);                    // i get NULL - why!?
$loggedin=$user->get_session('logged');     // i get the error message "call to a member function on a non-object
$data=$user->simple();
Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

Check the quotes on line 5: $this->set_session('logged', true);

Member Avatar
newprimitive
Newbie Poster
21 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Check the quotes on line 5: $this->set_session('logged', true);

I made mistake typing here.

How do I solve this problem:
When i want to

echo $user->logged;

i get error message saying: Trying to get property of non-object
Why can't I echo this attribute?
Also, when I want to call some Connect method I get error message: Call to a member function .. on a non-object.
The method I want to call is super simple, like this one:

function get_text(){
   return 'a';
}

Note that I didn't have any problems when was testing offline (locally), but when I put this code online I got these messages.

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

Your code seems ok to me. Maybe this comment can be useful for you: http://www.php.net/manual/en/language.oop5.basic.php#92759
bye.

Member Avatar
cwarn23
Occupation: Genius
3,028 posts since Sep 2007
Reputation Points: 387 [?]
Q&As Helped to Solve: 281 [?]
Skill Endorsements: 15 [?]
Team Colleague
Featured
 
0
 

You also didn't assign the class correctly. Try replacing with the following.

$user=new Connect(); //add the two brackets
Member Avatar
mschroeder
Work Harder
655 posts since Jul 2008
Reputation Points: 251 [?]
Q&As Helped to Solve: 134 [?]
Skill Endorsements: 8 [?]
Team Colleague
 
0
 

@newprimitive
Please post your entire Connect class. It appears you're missing a large portion of it, even if you're getting an error on one of the lines you posted, the fact that you're calling $this->set_session() and $this->get_session() and those functions are not presented it is impossible to debug your code properly.

There is a good chance that your error reporting levels differ from local to remote hosts. Or, possibly your datasets are different and the object is not getting populated correctly somewhere earlier.

@cwarn

Initializing a class without the parenthesis is a perfectly legitimate practice when you are not passing values into a constructor. Personally I always use the parenthesis for consistency so I agree with your statement, but it is not wrong.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: