Possible Duplicate:
Call to a member function on a non-object - works localhost but not online
don't know how to solve this problem, it's basic but can't find a solution. I use two files: connect.php and user.php.
in connect.php is a Connect class:
class Connect{
var $logged;
function login($username, $password){
..more code..
if($pass==$password){
$this->logged=true; // db connection is fine, this works
// checked it with echo $this->logged;
}
}
}
and when i call it from another file, user.php like this:
$user=new Connect;
$user->login();
echo $user->logged; // ERROR Trying to get property of non-object
Why is this code not working, but it works offline (locally)???
You've got a couple of problems:
$user->login
, PHP is assuming you're accessing an object property, not the function; you need $user->login()
(note the ()
), which will call the method.}
.Demo:
<?php
class Connect{
var $logged;
function login($username, $password){
$pass = 'test';
if($pass == $password){
$this->logged = true;
}
}
}
$user = new Connect;
$user->login('test','test');
print_r($user);
?>
Outputs:
Connect Object
(
[logged] => 1
)
1
is what true
prints.
To access a class property from outside the class, you need to declare it as 'public':
class Connect{
public $logged;