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();

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

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.

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

$user=new Connect(); //add the two brackets

@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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.