This question already has an answer here:
I'm currently working on a application which pulls JSON from a database and then adds a new user to the array and returns it as JSON. When I tried running my addFriend() function I got the "Call to undefined function userExists()" error. I can't seem to figure out why.
public function userExists($user = null){
if(is_numeric($user)){
$test = $this->_db->get('members', array('ID', '=', $user));
if($test->count()) {
return true;
}
}
return false;
}
public function addFriend($friend){
if(is_numeric($friend)){
if( userExists($friend) ){
if(hasFriends()){
$friendsList = getFriends();
$friendsList[] = $friend;
}else{
$friendsList = array($friend);
}
}
}
return false;
}
I am using a PDO class I made to call the queries and make the connection, which seems to be working fine. count()
just returns the rowCount()
.
I know it's going to be something small I missed but the help will be appreciated!
</div>
If you are inside the class, replace
if( userExists($friend) ){
with
if( $this->userExists($friend) ){
Basically to refer to pretty much anything inside the class, append $this->
to whatever you are calling, a property, a function etc.
It tells the class what to actually use, ie its own function or its own properties.
On that note, looking at the rest of your code, rather than using variables, I would suggest creating and using properties of the class.
For example, you have a variable called $friendsList
in your addFriend
function. It might be better to declare the class with a property called $friendList
like this:
class someThing{
public $friendlist;
// etc
}
and then in your functions, append or delete data from that property like this:
if( userExists($friend) ){
$this->friendsList = getFriends();
$this->friendsList[] = $friend;
}else{
$this->friendsList = array($friend);
}
In this way, once you assign the values, they are there and available for any other call or function to use as needed.