0

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!

Livewire
  • 180
  • 1
  • 1
  • 9

1 Answers1

2

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.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • Not to forget - The same thing needs to be done for `hasFriends()` and `getFriends()` too – asprin May 19 '14 at 10:44
  • *Facepalm* of coarse! Time for another coffee I suppose, thanks for the help! – Livewire May 19 '14 at 10:45
  • @Livewire See the addition I made, I think you might do well to change some of the class structure as well :) – Fluffeh May 19 '14 at 10:50
  • @Fluffeh So I should define the Friend List as a Class variable so it is accessible to all the methods in the class without having to query the database each time, great idea. I do already have a few of these but that is a great idea to make my application tidier and more efficient! I love this community when I can get feedback which helps my practices! – Livewire May 19 '14 at 11:09
  • @Livewire Glad that we could help you write better code mate :) – Fluffeh May 19 '14 at 11:17