I have a class.php
file, in which I include a class from another file:
class.php:
include($_SERVER['DOCUMENT_ROOT'] . "/includes/user.php");
$user = new User;
So, inside /includes/user.php
is the $user
class defined.
And in /includes/user.php
there is a function named getSocialUser($username);
:
function getSocialUser ($username)
{
global $dbh;
$stmt = $dbh->prepare("SELECT * FROM users WHERE username=:username");
$stmt->bindParam(':username', $username);
$stmt->execute();
$udata = $stmt->fetch();
if(count($udata) == 0)
return false;
return $udata;
}
(The $dbh
holds the database value. I've tested it, and it does have a connection to the database)
However, I want to be able to use that function above inside the class.php
file:
This is what I am doing now:
class.php:
include($_SERVER['DOCUMENT_ROOT'] . "/includes/user.php");
$user = new User;
Class feed {
function randomFunction($username) {
global $user;
$username = "test";
$viewuser = $user->getSocialUser($username);
echo $viewuser['avatar'];
}
}
The above code doesn't work. I get this error:
Fatal error: Call to a member function getSocialUser() on a non-object in/path/class.php on line 6
What am I doing wrong?